samedi 25 septembre 2021

if conditional statement doesn't appear to be working

So I am learning JavaScript. I am doing an assignment on conditional statements. We have to create a variable named randomNum and have it return a a value that is a whole number of 1 through 5.

I've got this:

let randomNum=Math.floor(Math.random()*5)+1;
console.log(randomNum);

which works.

I then have to create an if statement that displays "Not equal to 3" if the value for randomNum is NOT equal to 3.

I did this:

if (randomNum != 3) {
    console.log("Not equal to 3");
}

However, when the value returns as 3 it still logs "Not equal to 3". Am I doing something wrong? I feel as if I am missing something here.

Not sure if it matters but for a previous question I have in the same file this:

if (randomNum >= 4) {
    console.log("Greater than or equal to 4");
} else if (randomNum = 2, 3) {
    console.log("Equal to 2 or 3");
} else {
    console.log("Equal to 1");
}

This works just fine.

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire