JSFiddle - React, Tailwind, and code Playground

by BurpmanJunior

JavaScript

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!