My First Project with JS, and I'm stucked with Switch lol

Hello everyone. So I tried creating my own project after learning about switch. I can’t seem to get it to choose the computer wins || human wins output. Please help? Thanks!

// Computer VS Human
let computerChoice = 2;
let humanChoice = 1;

//Compare the outcome for the choices using switch
let outcome = computerChoice - humanChoice;
switch (outcome) {
case (outcome > 0):
console.log(‘Computer Wins!’);
break;
case (outcome < 0):
console.log(‘Human Wins!’);
break;
case (0):
console.log(‘Draw!’);
break;
default:
console.log (‘Please choose a number.’);
}

switch and case can only check whether a value is equal, not whether it is more or less.
case (outcome > 0): would end up doing the same as case true: or case false:
depending on whether outcome > 0 is true or false.

In this situation, it may be better to use if, else if, and else .

1 Like

I see. I had just explored switch and case, and thought it was just another way to simplify if else. Thank you for the explanation! =)

** Updated **

// Computer VS Human
let computerChoice = 2;
let humanChoice = 1;

//Compare the outcome for the choices using switch
let outcome = computerChoice - humanChoice;
// switch (outcome) {
// case (outcome > 0):
// console.log(‘Computer Wins!’);
// break;
// case (outcome < 0):
// console.log(‘Human Wins!’);
// break;
// case (0):
// console.log(‘Draw!’);
// break;
// default:
// console.log (‘Please choose a number.’);
// }
if (outcome > 0) {
console.log(‘Computer Wins!’);
} else if (outcome < 0) {
console.log(‘Human Wins!’);
} else if (outcome === 0) {
console.log(‘Draw!’);
} else {
console.log (‘Please choose a number.’);
}