logo CodeStepByStep logo

rockPaperScissors

Language/Type: JavaScript if/else console output
Author: Melissa Hovik (on 2016/09/25)

Write a function named rockPaperScissors that takes two parameters: a string representing a first player's move in a game of Rock-Paper-Scissors and a string representing a second player's move. Depending on each player's move, the function should output to the console the winner of the round if there was a winner, a tie if both players had the same move, or whether a player gave an invalid move. When comparing moves, letter-casing does not matter ("rock" should be considered the same move as "rOcK").

The two-player game of Rock-Paper-Scissors works as following:

  • Each player chooses one of three moves: "rock", "paper", or "scissors"
  • A move of "rock" wins over another player's move of "scissors"
  • A move of "scissors" wins over another player's move of "paper"
  • A move of "paper" wins over another player's move of "rock"
  • If two players use the same move, the result is a tie

For example, a call of rockPaperScissors("rock", "scissors") would result in console output of "Player 1 wins!". A call of rockPaperScissors("ROCK", "Paper") would result in console output of "Player 2 wins!". A call of rockPaperScissors("SCISSORS", "Scissors") would result in console output of "TIE!". If one player gives a move other than "rock", "paper", or "scissors", the console output should be "Invalid move of [player's invalid move]!", where you should replace [player's invalid move] with the exact value passed by that player. If both players pass invalid moves, the console output should be "Invalid moves of [player 1's invalid move] and [player 2's invalid move]!".

Function: Write a JavaScript function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.