Project: Rock paper scissors
Make a basic version of the classic game, "Rock, Paper, Scissors" with JavaScript.
Building a rock paper scissors game
We're going to make a simple implementation of grade-school classic "rock paper scissors". If you don't know what that is check the Wikipedia article or this ridiculous step-by-step. For the moment we're just going to play the game from the browser console, but we will revisit this project in a later lesson and add a Graphical User Interface with buttons and text, so don't forget to keep the code on GitHub!
Quick exercises before starting
Identify three ways to include JavaScript in a page.
Test it out! Write
console.log("Hello World");
in JavaScript and check to see if it displays in the browser's console.If you haven't used it already, this is where the VSCode Live Server extension becomes handy!
Finally, this is your first JavaScript program built from scratch, so don't forget the previous lesson on problem solving. Plan your solution out before writing any code, and test each piece as you build to ensure it is working before moving on to the next!
Assignment
Start a new Git repo for your project.
Create a blank HTML document with a script tag (Hint: it is best practice to link an external .js file). This game is going to be played completely from the console, so don't worry about putting anything else in there.
Your game is going to play against the computer, so begin with a function called
computerPlay
that will randomly return either 'Rock', 'Paper' or 'Scissors'. We'll use this function in the game to make the computer's play. Tip: use the console to make sure this is returning the expected output before moving to the next step!Write a function that plays a single round of Rock Paper Scissors. The function should take two parameters - the
playerSelection
andcomputerSelection
- and then return a string that declares the winner of the round like so:"You Lose! Paper beats Rock"
1. Make your function's playerSelection parameter case-insensitive (so users can inputrock
,ROCK
,RocK
or any other variation).Important note: you want to
return
the results of this function call, notconsole.log()
them. To test this function console.log the results:Write a NEW function called
game()
. Use the previous function inside of this one to play a 5 round game that keeps score and reports a winner or loser at the end.You have not officially learned how to "loop" over code to repeat function calls... if you already know about loops from somewhere else (or if you feel like doing some more learning) feel free to use them. If not, don't worry! Just call your
playRound
function 5 times in a row. Loops are covered in the next lesson.At this point you should be using
console.log()
to display the results of each round and the winner at the end.Use
prompt()
to get input from the user. Read the docs here if you need to.Feel free to re-work your previous functions if you need to. Specifically, you might want to change the return value to something more useful.
Feel free to create more "helper" functions if you think it would be useful.
Last updated