Ad
Fundamentals
Games

Implement function for Rock Paper Scissors game, players submits his/her shape and cpu gets random draw between the 3 shapes, the two data are compared and winner is decided.

I'm in learning fundamental JavaScript phase, tried to challenge my self to comeup with the code from syntax I know up till now, this is why its probably so long, tried the switch statement, i also did it in if/else statement, but liked this switch way cause i could include the shapes of players in the alert msg.

I checked the code and it works, but somehow cant figure out how to setup 'Test Cases'section for it on this website, appreciate if you guys could let me know how to set test cases for it or any other suggestions about my long & probably stupid code.

let player;
let cpu;

const rpsGame = function (player, cpu) {
  player = prompt(`Rock Paper Scissors Game, which shape you go with:`);
  cpu = Math.trunc(Math.random() * 3 + 1);
  switch (player) {
    case 'Rock':
      if (cpu === 2)
        return alert(`CPU's Paper covers your ${player}: YOU LOOSE!!!`);
      if (cpu === 3)
        return alert(`Your ${player} crushes CPU's Scissors: YOU WIN!!`);
      if (cpu === 1)
        return alert(`Both players choose ${player}: ITS A TIE...!`);
      break;
    case 'Paper':
      if (cpu === 1)
        return alert(`Your ${player} covers CPU's Rock: YOU WIN!!`);
      if (cpu === 2)
        return alert(`Both players choose ${player}: ITS A TIE...!`);
      if (cpu === 3)
        return alert(`CPU's Scissors cuts your ${player}: YOU LOOSE!!!`);
      break;
    case 'Scissors':
      if (cpu === 1)
        return alert(`CPU's Rock crushes your ${player}: YOU LOOSE!!!`);
      if (cpu === 2)
        return alert(`Your ${player} cuts CPU's Paper: YOU WIN...!!`);
      if (cpu === 3)
        return alert(`Both players choose ${player}: ITS A TIE...!`);
      break;
    default:
      return alert(`Wrong! Please choose from Rock, Paper or Scissors`);
      break;
  }
};