Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Modulus Operator

shannondingleshannondingle Member Posts: 10
HI,
I have this piece of code, could anyone explain to me what it is doing. I don't understand "count % 2 ==1?" What is the significance of the question mark.

Shannon


Example1


var count = 1;

while (count <=8) {
document.writeln(count % 2 ==1? "XXXXX<br />" : "$$$$
");
++count;
}






Comments

  • zibadianzibadian Member Posts: 6,349
    : HI,
    : I have this piece of code, could anyone explain to me what it is doing. I don't understand "count % 2 ==1?" What is the significance of the question mark.
    :
    : Shannon
    :
    :
    : Example1
    :
    :
    : var count = 1;
    :
    : while (count <=8) {
    : document.writeln(count % 2 ==1? "XXXXX<br />" : "$$$$
    ");
    : ++count;
    : }
    :
    :
    :
    :
    :
    :
    :
    That's part of a trinary operator, which does the same thing as an if-then statement. Here's an example:
    [code]
    a = b==2?1:3
    [/code]
    If b equals 2 then a will be assigned 1 otherwise 3. The ?-symbol has nothing to do with the %-operator.
Sign In or Register to comment.