Created
October 23, 2020 12:36
-
-
Save RaynZayd/62aac71ce8e2964f96ef30933e7dd717 to your computer and use it in GitHub Desktop.
Game code is broken down into four parts: 1. Get user’s choice. 2. Get computer’s choice. 3. Compare two choices and determine the winner. 4. Start the program and display the results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Game code is broken down into four parts: | |
1. Get user’s choice. | |
2. Get computer’s choice. | |
3. Compare two choices and determine the winner. | |
4. Start the program and display the results | |
*/ | |
//Const arrow function | |
/*The user should be able to choose ‘rock’, ‘paper’, or ‘scissors’ when the game starts*/ | |
const getUserChoice = userInput => { | |
userInput = userInput.toLowerCase(); | |
//User can pass in a parameter, such as ‘Rock’ or ‘rock’ with different capitalizations | |
//Code below makes sure that the user typed a valid choice: ‘rock’, ‘paper’, or ‘scissors’. | |
if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){ | |
return userInput; | |
}else{ | |
console.log('Error!'); | |
} | |
} | |
/*The lines below are for testing purposes Tests the function by calling it with valid and invalid inputs, and printing the results to the console.*/ | |
//console.log(getUserChoice('PAPER'));Prints paper | |
//console.log(getUserChoice('fork'));Prints error! | |
//Function for computer's choice | |
const getComputerChoice = () => { | |
var randomNumber = Math.floor(Math.random()*3);//Random number is for 3 choices between 0 and 2 | |
switch(randomNumber){ | |
case 0: | |
return'rock'; | |
case 1: | |
return'paper'; | |
case 3: | |
return'scissors'; | |
} | |
} | |
/*Below is code that test the function by calling it multiple times and printing the results to the console.*/ | |
//console.log(getComputerChoice()); | |
//console.log(getComputerChoice()); | |
//console.log(getComputerChoice()); | |
//This function determines the winner based on user and computer choice | |
const determineWinner = (userChoice,computerChoice) => { | |
if( userChoice === computerChoice){ | |
return 'The game was a tie!'; | |
} | |
if(userChoice === 'rock'){ | |
if(computerChoice === 'paper'){ | |
return 'Computer won!'; | |
}else{ | |
return 'User won!'; | |
} | |
} | |
if(userChoice === 'paper'){ | |
if(computerChoice === 'scissors' || 'rock'){ | |
return 'Computer won!'; | |
}else{ | |
return 'User won!'; | |
} | |
} | |
if(userChoice === 'scissors'){ | |
if(computerChoice === 'rock' || 'paper'){ | |
return 'Computer won!'; | |
}else{ | |
return 'User won!'; | |
} | |
} | |
} | |
//console.log(determineWinner('paper','scissors')); To test function | |
//Function to start the game | |
function playGame(){ | |
var userChoice = getUserChoice('rock'); | |
var computerChoice = getComputerChoice('scissors'); | |
console.log(userChoice,computerChoice);//Choices made | |
console.log(determineWinner(userChoice,computerChoice));//Determines winner | |
} | |
console.log(playGame()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment