Skipping line before time

Asked

Viewed 39 times

0

<script>

    var i = 0;

    while(i < 100){

        n = 1;
        resultado = n + i;

        if(0 === (i%10)){

            document.write(n + '-' + resultado + '<br>');

        }else{
        document.write(n + '-' + resultado + ' ');
        }


        i++;
    }
</script>

I’m typing this code for an exercise, and it’s still part of what I want to do, but I found a problem, he’s giving the if command to jump the line before the time, it was only supposed to be in the 1-10, but he does it in 1-1, I hope you can understand, I’m sorry if I expressed myself badly, but I’m starting to study this area now, thanks!

  • I tried to change var i for 1 and even so, now it break on line 1-11 not on line 1-10 as I would like.

  • Zero gives true at "0 === 0 % 10" you have to add "&& i > 0". You want there to be line break every 10 right?

2 answers

0


As you have been told in the comments, the first iteration is already 0, so your line break.

If you start at 1 and replace the < 100 to: <= 100, the counter will work.

The code I tested:

<script>
    var i = 1;
    while (i <= 100) {
        if (0 === (i % 10))
            document.write('1 -' + i + '<br>');
        else
            document.write('1 -' + i + ', ');
        i++;
    }
</script>

0

You can just change the if to 0 === ((i+1)%10) and keep the rest of your logic.

<script>

    var i = 0;

    while(i < 100){

        n = 1;
        resultado = n + i;

        if(0 === ((i+1)%10)){

            document.write(n + '-' + resultado + '<br>');

        }else{
        document.write(n + '-' + resultado + ' ');
        }


        i++;
    }
</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.