If/else vs switch statements

(https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/learn-javascript-functions/projects/rock-paper-scissors). i have been working on the above link. on step 5 i used switch statement successfully but wasnt successful with if/else statement. please what could be the issue?

1 Like

Your getComputerChoice should look something like this:

const getComputerChoice = () => {
  let randomNumber = Math.floor(Math.random() * 3);
  if (randomNumber === 0) {
    return 'rock';
  } else if (randomNumber == 1) {
    return 'paper';
  } else if (randomNumber == 2) {
    return 'scissors';
  }
};

if-s are different from switch-es. If using if than you have to set each of them. They’re good if you want to choose between different things. Switch-es can be used here and i would do so in this case And for example they are useful can be useful in a game where you have to move the character using the keyboard:

switch (e.keyCode) {
   case 39:
	//right key
	xPosr = xPosr + 5;
   case 37:
	//left key
	xPosr = xPosr - 5;
   case 38:
	//up key
	yPosr = yPosr - 5;
   case 40:
	//down key
	yPosr = yPosr + 5;
}

But with using if:

if (e.keyCode == 39) {
	//right key
	xPosr = xPosr + 5;
	}
	else if (e.keyCode == 37) {
		//left key
		xPosr = xPosr - 5;
	}
	else if (e.keyCode == 38) {
	   //up key
	   yPosr = yPosr - 5;
	}
	else if (e.keyCode == 40) {
	   //down key
           yPosr = yPosr + 5;
	}
1 Like

Very helpful. From your explanation I think i have discovered my mistake. It is the equality sign(=).
i picked up while learning javascript here on codecademy that you use a single equality(=) for numbers and triple for strings(===). but your code above contradicts thats. You made use of both single and triple equality for numbers.
my code looked like so:
if (randomNumber = 0) {
return ‘rock’;
} else if (randomNumber = 1) {
return ‘paper’;
} else if (randomNumber = 2) {
return ‘scissors’;
}
please review and explain.

2 Likes

Some coding platforms for example Codepen wants you to use === when comparing to 0:
image

‎What you saying is === works for numbers aswell as strings. Right?

Yes it works but they’re different == means “equal to” but === means “equal value and equal type”
!= means “not equal” but !== means “not equal value or not equal type”

let x = "0";
console.log(x == 0) //prints true to the console because they're equal
console.log(x === 0) //prints false to the console
console.log(x != 0) //prints false to the console
console.log(x !== 0) //prints true to the console

Think === as and statements, false + true = false, true + true = true, false + false = false (I mean equal value and equal type),
they return true if both expressions are true, otherwise it returns false.
think !== as or statements, false + false = false, true + true = true, true + false = true, (I mean not equal value or not equal type)
they return true if one or both expressions are true, otherwise it returns false.

Hope this helps :grinning:

i dont really understand this but i will research more on it. your help is appreciated.

Are you clear with logical operators? Anyway here’s a link: https://www.w3schools.com/js/js_operators.asp
They write it clearly down.