| Viewed 268 times

Simulate the Game "Rock, Paper, Scissors" in javaScript?

Create a function which simulates the game "rock, paper, scissors". The function takes the input of both players (rock, paper or scissors), first parameter from first player, socond from second player. The function returns the result as such: "Player 1 wins" "Player 2 wins" "TIE" (if both inputs are the same)

1 Answer
Mohammad Qandeel


This is a function that receives two variables, the first variable for the first player, the second variable for the second player

The value can be rock, paper, scissors

const rps = (s1, s2) => {
 if (s1 === s2) return "TIE";
 let res = {p: "rock", s: "paper", r: "scissors"};
 return res[s1[0]] === s2 ? "Player 1 wins" : "Player 2 wins";


rps('scissors','scissors') // "TIE"

rps('rock','scissors')     // "Player 1 wins"

rps('scissors','rock')     // "Player 2 wins"


Related Questions