0

I was trying to print a number 1 to 3 and when it becomes multiple by 3, it will create a <br> and then continue until it meets the number 185.
I try this code but it gives me a wrong answer:

for (a = 1; a <= 185; a++) {
  document.write(" ", a);
  if (a % 3) {
    document.write("<br>");
  }
}

But it gives me this answer:

 1<br>
 2<br>
 3 4<br>
 5<br>
 6 7

Instead of:

 1 2 3<br>
 4 5 6<br>
 7 8 9
nempoBu4
  • 6,521
  • 8
  • 35
  • 40

5 Answers5

2

This is doing the opposite of what you want and printing out a <br> when a is not a multiple of three. What you want is:

if(a % 3 === 0)

The reason

The body of an if will execute when the expression betweeen the parentheses is "truthy" (anything other than 0, false, undefined, null, the empty string, or NaN). When a is not a multiple of 3, a % 3 will be 1 or 2, which is "truthy", and when it is a multiple of 3, a % 3 will be 0, which is "falsy". What you want is the opposite of that.

Given the above explanation, you could also use this:

if (!(a % 3))

, but I would say that the longer version above far more clearly conveys your intent and I would suggest using that.

JLRishe
  • 99,490
  • 19
  • 131
  • 169
1

Use if(a % 3 == 0) instead of if(a % 3)

<script>

for(a=1;a<=185;a++)
{
    document.write(" ",a);
    if(a % 3 == 0)
    {
        document.write("<br>");
    }
}

</script>
John Bupit
  • 10,406
  • 8
  • 39
  • 75
Alex Vazhev
  • 1,363
  • 1
  • 18
  • 17
  • thank you sir. it works too..^^ this is my problem for almost 2 weeks. good thing i have a chance to post my problems here.. thanks sir.. have a good day.. – Hooded warlock Jan 27 '15 at 04:03
1

Another way:

(function(){
    var s= [], a= 1;
    while(a<185){
        s.push([a++, a++, a++].join(', '));
    }
    document.write(s.join('<br>\n'));
})();

//thanks to JB

kennebec
  • 102,654
  • 32
  • 106
  • 127
0

The expression 'a % 3' will return 0 if a is divisible by 3, and the remainder if it is not.

The test if( x ) will evaluate the body if x is non-zero, and will skip the body (and evaluate the else clause if it is present) if x is zero (or something like a zero, such as undefined).

Your best bet, in case boolean evaluation case (such as an if), is to be very explicit about what you're testing it for. In your case, you probably want to explicitly test if( a % 3 === 0)

Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • thanks for answering sir. can i ask what is the difference between a % 3 ==0 to a % 3===0. i tried it bot, and it gives me the correct answer, which is correct. just wondering – Hooded warlock Jan 27 '15 at 04:07
  • In this case, both of them should work. In other cases, the === is like an ==, but also checks to make sure the type of the items being compared are the same. In most cases, you'll want to use ===. See http://stackoverflow.com/questions/523643/difference-between-and-in-javascript for more info – Prisoner Jan 27 '15 at 11:58
0

in the if condition use a%3==0 to get proper result. Rest of your logic is good

for(a=1;a<=185;a++)
{
document.write(" ",a);
if(a % 3 == 0) /// <----- change is here
{
document.write("<br>");
}
}
sabkaraja
  • 342
  • 4
  • 15
  • thanks for helping sir. if i can just give you a big hug, i will give you some for 2hours.. thanks again sir.. have a greate day..^^ – Hooded warlock Jan 27 '15 at 04:04