Display each set of three numbers on a new line, separated by commas

I need JavaScript code that generates 3 numbers from 1 to 10 hundred times. It should display each set of 3 numbers in a new line, separated by commas, and also display the total number of times when the generated numbers were 7 (also on a separate line). An example output should be formatted as follows: Numeric value 1: 10,7,8 Numeric set 2: 5,1,7 Code that for some reason I do not work

<html> <head> <title>Day 3 - Example 7</title> </head <body> <script language="javascript"> // count number of times seven was generated var i,num,n,num1,num2,cnt=0; n=100; for( i=1; i<=n; i++){ num = Math.floor(Math.random()*10+1); num1 = Math.floor(Math.random()*10+1); num2 = Math.floor(Math.random()*10+1); document.write("Number Set " +i+ is + num,+ num1, +num2); if (num == 7) { cnt++; > } } document.write("<br>Total number of Sevens: " + cnt); </script> </body> </html 
+5
source share
7 answers

You need to specify the lines in your call to document.write() .

 document.write("Number Set " +i+ " is " + num + ", " + num1 + ", " +num2 + '<br>'); 
+1
source

The code you posted seems to be correct based on your description, with the exception of a few minor errors. I cleaned it up and made it available in Stack Overflow.

 var i, num, n, num1, num2, cnt = 0; n = 100; for (i = 1; i <= n; i++) { num = Math.floor(Math.random() * 10 + 1); num1 = Math.floor(Math.random() * 10 + 1); num2 = Math.floor(Math.random() * 10 + 1); console.log("Number Set", i, "is", num, num1, num2); if (num == 7) { cnt++; } } console.log("<br>Total number of Sevens: " + cnt); 

Note. . When you use + between a line and a number, as you did, the number will be converted to a line, and then two lines will be added. Comma ( , ) will add values ​​separated by spaces. I used commas to make them clear and consistent. I was also a little upset for readability and used console.log so that we could see the result of formatting changes.

+1
source

There are a couple of problems in the code:

1) You are trying to send a string to document.write, but you did not format it correctly . Everything that is just a string, not a variable of any type, must be enclosed in quotation marks. In your case, this includes: 'is' and commas (',').

(I put each line in the <p> tag to make sure it is printed on a new line)

2) At the end of your code there is also a wandering symbol > that needs to be removed.

Here is an example of the modified code:

 var i,num,n,num1,num2,cnt=0; n=100; for( i=1; i<=n; i++){ num = Math.floor(Math.random()*10+1); num1 = Math.floor(Math.random()*10+1); num2 = Math.floor(Math.random()*10+1); document.write("<p>Number Set " + i + " is " + num + ", " + num1 + ", " + num2 + "</p>"); if(num == 7){ cnt++; } } document.write("<br>Total number of Sevens: " + cnt); 
+1
source

you have a couple of problems:

  • the <head> element is not closed properly.
  • You have some syntax errors in most areas.

Consider this figure below:

 <html> <head> <title>Day 3 - Example 7</title> </head> <body> <script language="javascript"> // count number of times seven was generated var i,num,n,num1,num2,cnt=0; n=100; for( i=1; i<=n; i++){ num = Math.floor(Math.random()*10+1); num1 = Math.floor(Math.random()*10+1); num2 = Math.floor(Math.random()*10+1); console.log("Number Set " +i + " is --> "+ num +", "+ num1 +" ," +num2); if(num == 7){ cnt++; } } document.write("<br>Total number of Sevens: " + cnt); </script> </body> </html 

you can replace console.log() as follows:

 document.write("Number Set " +i+ " is " + num + ", " + num1 + ", " +num2); 
+1
source

 var n = 100, counter = 0; for (var i = 1; i <= n; i++) { // repeat this n times var text = "Number Set " + i + " is:"; // text of this set for(var j = 0; j < 3; j++) { // repeat this 3 times var num = Math.floor(Math.random() * 10) + 1; // get a random number between 0 an 10 text += (j? ", ": " ") + num; // add it to this set text if(num === 7) // if the random number is seven counter++; // then increment the counter } document.write(text + "<br>"); // show this set text } document.write("Total number of Sevens: " + counter + "<br>"); // the seven count 
+1
source

Use this line: document.write("Number Set "+i+" is "+ num +","+num1 +","+num2);

0
source

Enough commentators have indicated that erros are written in the document - although I would just add a version that solves the problem with a bit more elegance.

 var setSize = 3, totalSize = 100, magicNumber = 7, magicCounter = 0, i = setSize * totalSize, set = []; do { // push random number into set set.push(Math.floor(Math.random() * 10 + 1)); // decrement the counter i -= 1; // jump to the next iteration, if the set is not complete if (set.length !== setSize) { continue; } console.log("Number set is", set.join(', ')); // reduce the array into the sum of items and check if equal 7 if (set.reduce(function(a, b) { return a + b; }, 0) === magicNumber) { magicCounter += 1; } // reset the set for the next line set = []; } while (i > 0); console.log("Total number of ", magicNumber, ":", magicCounter); 
0
source

Source: https://habr.com/ru/post/1266486/


All Articles