-3

I want to break my for loop that I have 10 numbers in a line.

for(var i = 100; i <=300; i++){
    console.log(i);
}
Mark
  • 90,562
  • 7
  • 108
  • 148

3 Answers3

1

I guess by "that I have 10 numbers in a line." you mean to break the line after 10 printed numbers. It seems not to be possible to log to the console without a linebreak, so you have to concatenate the output until 10 outputs are concatenated.

var line = '';              // initialize line variable
for(var i = 100; i <=300; i++) {
    line += i               // append current value to the line without printing it
    if ((i%10) === 0) {       // check, if the current iteration is dividable by 10
        console.log(line);    // output the collected output
        line = ''             // reset the line var
    }
}
if (line !== '') console.log(line);    // if the total count was not dividable
                                       // by 10, output the left over
  • Hi. Your answer didn’t really answer my question as the question is not straightforward. But I was wondering if you could comment this code. I’d like to investigate it. Thanks. – Richard Obaseki Dec 26 '18 at 20:42
  • Added some comments :) – DeoxyribonucleicAcid Dec 26 '18 at 21:25
  • Thanks for the comments. I’m practicing on something and I used this code to get a line of numbers where when I input a given integer, I get steps of that number counting downward like 1, next step 2, next step 3, and so on. I want to know if you can give a code where I can get the same steps of downward numbers, but backwards. I.e 1, next step 2 backward of 1 not forward. I don’t know if you understand. – Richard Obaseki Dec 28 '18 at 20:33
0

In response to your statement, "My loop starts from 100, when it gets to 110, it breaks and skip a line. And continue reading when it reaches 120, skips a line:

        for(let i = 100; i <=300; i++){                         
            if(i % 10 == 0) {               
                continue;
            }           
            console.log(i);
        }

A break will exit the loop completely, a continue will skip to the next iteration. This skips every loop iteration that's divisible by 10.

Artanis
  • 561
  • 1
  • 7
  • 26
-1

you can use if condition inside your for loop lets say if you want to break the loop after 10 numbers printed just add this line in your for loop

if(i==10)
{
   break;
}

The break condition will bring you out of the for loop.

Amir shah
  • 317
  • 2
  • 7