javascript - Displaying Prime Numbers Using document.write() -
I have an assignment for school that I think
write a script which is 1 And between 999, the Prime Minister prints the numbers in a table, which has 10 columns. To create tables, document elements and a> document to create counter variables. Use the written () statement so that it can create 10 columns. Counter> variable should start with the starting value of 0 and every time your> code recognizes a prime number and prints it in a table cell, then it should be increased. Once the counter variable> value of 10 (i.e. 10 cells have been added to the existing row), print> to start a new line and reset the variable to 0.
I can not detect where my error or errors with the table row and counter are, besides, I might have written the document in the wrong place. Currently, the table only displays vertically
What do I have here:
& lt; Script & gt; Function Primary Buzz (num) {document.write ('& lt; Table & gt;'); If (num & lt; 2) return false; (Var i = 2; i & lt; num; i ++) for {if (num% i == 0) returns false; } Back true; } Document.write ('& lt; tr & gt;'); Var counter = 0; {If (prime i) = (i & lt; 999; i ++) {If (Primary Banz (I)) {If (counter% 10 == 0) {document.write ('& lt; / tr & gt; ; & Lt; tr & gt; '); Counter = 0; } Counter ++; Document.write ('& lt; td>' + i + ''); }} Document.write ('& lt; / table & gt;'); & Lt; / Script & gt;
Please, press me in the right direction and do not answer me. I have to see that I can understand it.
Thank you!
There were some errors in your code:
- inside the function
& lt; Table & gt;
was an opener, so in fact it was opening 1000 times. - The counter is reset, so you do not need to use the modulus (
%
), just compare 10. - One
- Your function is incorrectly named
primenumbers
, the best callisPrime
here Code:
function isPrime (num) {if (num & lt; 2) return false; (Var i = 2; i & lt; num; i ++) for {if (num% i == 0) returns false; } Back true; } Document.write ("& lt; Table & gt; & lt; tr & gt;"); Var counter = 0; For (var i = 0; i & lt; 999; i ++) {if (prime): {if (counter == 10) {document.write ('& lt; / tr & gt; & lt; ; Tr & gt; '); Counter = 0; } Counter ++; Document.write ('& lt; td>' + i + ''); }} Document.write (' & lt; / table & gt;');
Here's the work of Bela (I can not use the document. Write here but you get this idea)
Comments
Post a Comment