var compare = function(choice1, choice2){
// Check for a tie
if(choice1 === choice2){
// return will cancel any further processing in the function so the rest of this function won't run if there's a tie.
return "The result is a tie!";
}
// If it's not a tie then we can check for each case.
// Switch is like a ton of if elses.
// If a case matches the variable in switch() then it runs that.
// break; is used to stop it trying to run something else after it's matched something. You can keep it if you want to match multiple things and run multiple statements.
switch(choice1){
case 'rock': // Check rock
// This is an inline if statement. Basically: (comparison ? if is true : if is not)
return (choice2 === 'scissors' ? 'rock wins' : 'paper wins');
break; // Technically not needed because there's a return but whatever, good practice.
case 'scissors': // Next case to check if the one above doesn't match
return (choice2 === 'paper' ? 'scissors wins' : 'rock wins');
break;
case 'paper': // You get it
return (choice2 === 'rock' ? 'paper wins' : 'scissors wins');
break;
}
}
console.clear(); // Clears console
console.log(compare('scissors', 'paper')); // scissors wins
console.log(compare('rock', 'paper')); // paper wins
console.log(compare('rock', 'scissors')); // rock wins
console.log(compare('rock', 'rock')); // The result is a tie!
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.