Recently, I have to write a program which, when I have received 5 values in my document.write
, the program has to start on a new line. I searched on multiple things on the internet and I found nothing about it. Here I show you how to deal with it. Hope it can help you!
var countr = 0;
for(i = 1; i <= 50; i++) {
document.write(i+" ");
compteur++;
if (count === 5) {
document.write("<br>");
count = 0;
}
}
You want to use i % 5 === 0 -- Modulus will calculate the remainder. Use in an if statement to write <br>
when it returns 0.
//var countr = 0;
for (i = 1; i <= 50; i++) {
document.write(i + " ");
// compteur++;
if (i % 5 === 0) {
document.write("<br>");
// count =0;
}
}
Since you're using a loop, you can use i
in your comparison as long as you don't update the variable. You're also using countr, compteur, count while you can use i
instead. // comments (disables) code.
I don't understand exactly what result you are expecting. But at first what I see - you have written your counter incorrect 3 times.
var counter= 0;
for(i=1 ; i<=50 ;i++){
document.write(i+" ");
counter++;
if (counter === 5){
document.write("<br>");
counter =0;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.