How to check in an if statement if a variable is even or odd

Hello,

I have written a code that increments a variable count by 1 on click. I want to then write an if/else statement that says: if the variable is even then execute code A, else execute code B. I know that usually you would write it like:
if (variable%2===0) {code A} else{code B} , however this doesn’t seem to work. Can someone help me out?

Thank you!

function oddOReven(value) {
   if (value%2 == 0) return true;
   else return false;
}

export function button1_click(event) {
    let VALUE = $w('#input1').value
    console.log(oddOReven(VALUE));
}

Test it on a new separate page (by taking a look into the CONSOLE!)

Or test it here…

https://russian-dima.wixsite.com/login-system/blank-1

This example will be deleted after 24-hours!

Hmm, I would think that the value would need to be converted to a Number before checking:

let VALUE = Number($w('#input1').value);

Sometimes I wish there was a like icon for answers as well.

Works also without conversion into a NUMBER :grin:

if(value%2==0) --> == and not ===

Some badly & dirty tricks xDDDDD :smiling_imp:

@russian-dima Don’t know why that works. Perhaps Javascript is smarter than I thought. (Or I’m dumber than I thought).

And, the difference between == and === always confuses me.
I tried with === and that also works.

I would convert to Number just to be sure.

@yisrael-wix
Yes of course your suggestion is the more secure one to go, but → since i do not use the (third → =) JS do not differ between STRING or NUMBER

@yisrael-wix I’m sure you know the difference between double and triple equals though :wink:

@russian-dima The thing is, == doesn’t check the value’s Type, but if the value being checked is not a Number, then there will be a problem. I guess this would work, but I’d be afraid.

@russian-dima It actually does differ, JavaScript will actually try to convert the values into a like type. In this case, it succeeds. The string value of ‘5’ can easily be converted into the number value of 5. Since 5 equals 5, we get our answer of true .

@ahmadnasriya ok, i should have chose the long way to solution and not the short and comfortable one xD

@ahmadnasriya Yeah, I realize that, and it makes sense. It’s just that I really don’t like assuming stuff. Guess I still have old habits left over from C/C++.

@russian-dima Nah - your short comfortable answer is just fine. I was just being obsessive-compulsive.

Hey guys - it was a nice discussion. Sometimes it’s good to air out issues to get the old grey matter working.

@yisrael-wix You can stay always obsessive-compulsive, there is surely more stuff to learn for me :grin:

Hey guys - it was a nice discussion. Sometimes it’s good to air out issues to get the old grey matter working.
Some REFRESH always good :sweat_smile:

Of course, then there’s Typescript.

Typescript!? Hmmm, don’t wanna open the next door, yet xDDDD.
First studiying all the already opened doors to almost 100%, and then… open end xD.

BTW: Thanks for AWARD! :roll_eyes::beers:

@russian-dima Nothing’s wrong with the double equals, as long as you’re expecting the values to be of like number type.

You really like gray :+1:

Hello all,

Thank you very much for the discussion. I probably should have been a little more specific with my question. I am trying to play a recording (audio) when a box is clicked and pause it when clicked again. I have written the code below, but it doesn’t seem to run the if statements:

var count = 0;

$w('#play').onClick((event, $w) => {
    count+=1;
    console.log(count)
});

if (count%2==0){
    $w('#recording').pause();
    console.log('even')
} else{
    $w('#recording').play();
    console.log('odd')
}


play is the box that is clicked and recording is the ID of an audio recording I have added.

When I print the count it shows up on the console correctly incrementing but the if/else statement doesn’t seem to run.

Thank you in advance!