What you will build

Rock-paper-scissors is a small project with several useful programming ideas inside it. Your program must accept a choice, reject anything it cannot use, make a choice for the computer, and compare the two. By the end, you will have a playable one-round game and a clear path for improving it.

You need a desktop browser with Developer Tools, a JavaScript console, and support for standard JavaScript features such as const and let. No library, account, or installation is required. Open a new browser tab, then open Developer Tools and select Console. This version uses the browser’s prompt dialog for input and prints the result in the console. To play another round, run the code again.

Turn the game rules into decisions

Both players choose one of three moves: rock, paper, or scissors. Matching moves produce a tie. Otherwise, rock beats scissors, scissors beats paper, and paper beats rock. Those are the only winning pairs you need to check from the player’s point of view. If the moves differ and none of those pairs matches, the computer wins.

Input needs attention before the game can use it. A player might type capital letters, add spaces, enter an unrelated word, or cancel the prompt. Trimming spaces and converting letters to lowercase lets inputs such as “ Rock ” work. Canceling or entering an invalid move should end the round without selecting a computer move.

Write and run the game

Enter the following JavaScript in the browser console. If your browser restricts pasting into Developer Tools, follow its on-screen guidance rather than bypassing a safety warning. The script plays one round each time it runs.

const choices = ["rock", "paper", "scissors"]; const input = prompt("Choose rock, paper, or scissors:"); if (input === null) { console.log("Round canceled."); } else { const player = input.trim().toLowerCase(); if (!choices.includes(player)) { console.log("Enter rock, paper, or scissors to play."); } else { const randomIndex = Math.floor(Math.random() * choices.length); const computer = choices[randomIndex]; console.log("You chose " + player + "."); console.log("The computer chose " + computer + "."); if (player === computer) { console.log("It's a tie!"); } else if ( (player === "rock" && computer === "scissors") || (player === "scissors" && computer === "paper") || (player === "paper" && computer === "rock") ) { console.log("You win!"); } else { console.log("The computer wins!"); } } }

Understand the choice and the result

The choices array keeps the valid moves in one place. The includes check stops the round when the player’s input is not in that array. This matters because an invalid word cannot be fairly compared with a computer move. The cancel check comes first: prompt returns null when canceled, and null has no trim method.

Math.random() produces a number from zero up to, but not including, one. Multiplying it by choices.length gives a number from zero up to, but not including, three. Math.floor turns that into an array index: zero, one, or two. Each index points to one computer move. This is a random selection, so running the script again may produce a different move.

The result checks are ordered deliberately. First, identical moves make a tie. Next, three conditions cover every way the player can win. At that point, if the input was valid and the game was not a tie or a player win, the remaining result is a computer win. The console prints both moves before announcing the result so you can check that the rule was applied correctly.

Test the rules and try an extension

Run several rounds with each valid move. Also try extra spaces, uppercase letters, an empty response, an unrelated word, and Cancel. Check that invalid input and cancellation produce a message rather than a result. Because the computer’s move is random, repeated rounds alone may not quickly show every winning pair. To inspect a specific case while learning, temporarily replace const computer = choices[randomIndex]; with const computer = "scissors";, then choose rock and verify that you win. Restore the original line afterward and repeat with other fixed moves if needed.

For a next step, keep scores across rounds. Begin with playerScore and computerScore set to zero, then add one to the appropriate score after a win. That change raises a useful design question: if you rerun the entire script, the scores start over. A longer game therefore needs a way to play multiple rounds while keeping the score variables alive.

Back to category ↗