computerChoice


#1

This is the code provided by codeacademy.
How is this working?
If random number is 0.12 for example...both conditions are true..0.12 < 0.34 and 0.12 <= 0.67.
Why does JS assigns "rock" to computerChoice and does not spit out an error.


var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
}


#2

but else if can only run if the if condition is false.

this are the possible paths:
1:

if true -> condition is met -> end

2:

if false -> else if true -> condition is met -> end

3:

if false -> else if false -> else -> condition is met -> end

so when if condition is true, else if will never run.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.