Hi !
Can anybody please tell me why my if... else
function always returns “The cards say yes” ? (I tried it on repl.it too, it gives out the same)
I watched the video, corrected my code which was more complicated, tried assigning ===
(and then the switch
and the if... else
functions rendered the same result but at least, it was random), nothing seems to work. What am I missing ? Thanks for your answer, if anybody can see one
Hello! The reason is in your if
statements, you use value = value1
. =
is the assignment operator, which means when you write if randomNumber = 0
, you are actually assigning 0
to randomNumber
.
That means, when you check if (randomNumber = 0)
, you are actually saying “if the value stored in randomNumber
-0
, is a truthy value, run the following code:”. 0
, however, is a falsy value. That means that the next condition is checked (else if (randomNumber = 1)
. This assigns 1
to randomNumber
, and then checks to see if that is truthy. Since 1
is a truthy value, the code inside the first else if
will always be run.
`=` vs `==` vs `===`:
=
:
=
is an assignment operator. It assigns values to variables:
let a = "value";
^
//assigning the value "value" to a
==
==
is a comparison operator, but it does not worry about types:
4=="4"
//this returns true, even though they are different types.
4 == 5
//this returns false
===
:
===
is a strict comparison operator. It compares the data and the type:
4 === "4"
//returns false since they are not they same type
4 === 5
//returns false as they are not the same values
4 === 4
//returns true as they are the same value and type
I hope this helps!
Thank you, codeneutrino, it helps a lot, I hadn’t thought about that !
I managed through it with the comparison operator <=
starting with 1 up to 7, and hopefully implying 0 in the else
statement : the code runs fine except for the else
that’s never taken into account, and I guess it’s because of the 0 falsy value, and the 7, maximum truthy value in the exercise.
But then, is this an example when you mustn’t use the if... else
statement and use the switch
instead ?
Or is there another way to work around it which I haven’t thought about, apart from making another declaration altogether ?
Do you mean kinda like this:
if (n <= 1) {
//code
} else if (n <= 2) {
//code
} else if (n <= 3) {
//code
} else if (n <= 3) {
//code
} else if (n <= 4) {
//code
} else if (n <= 5) {
//code
} else if (n <= 6) {
//code
} else if (n <= 7) {
//code
} else {
//last little bit of code
}
If so than as long as n
(or randomNumber
in your code) is not more than 7, the else
will never run. Take a look at the operator you are using: <=
. The less than or equal to operator will register as true
if the number on its left is the same as or less than the number on its right. Because of this 0 and 1 will both return the same in your code because:
>>> 0 <= 1
true
>>> 1 <= 1
true
You never HAVE to use the switch
as opposed to an if/else
as a switch
is just a simpler version of if/else
blocks using the equality operator ==
. However there are certainly times when it does save a lot of time and makes code more readable, making it best practice. For example instead of:
if (letter == 'a') {
//code
} else if (letter == 'b') {
//more code
} else if (letter == 'c') {
//continuing to type code
} else {
//can't forget the else code...
}
You could do this:
switch (letter) {
case 'a':
//code
break;
case 'b':
//code
break;
case 'c':
//code
break;
default:
//code
}
Thank you for your further explanation, 8-bit-gaming.
I thought about it and it suddenly dawned on me that in fact, when the switch
and the if... else
statements give together the same random result, it means it’s alright ?!! I had tried it but I thought I was wrong… Trust me to find difficulty where there isn’t any ! Thanks again !