Odin_RockPaperScissors/script.js
2023-01-17 18:50:15 +01:00

118 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//1.function called getComputerChoice
//that will randomly return either Rock, Paper or Scissors.
//2.function that plays a single round of Rock Paper Scissors.
//The function should take two parameters - the playerSelection and computerSelection -
//and then return a string that declares the winner of the round like so:
//"You Lose! Paper beats Rock"
//Make your functions playerSelection parameter case-insensitive
//(so users can input rock, ROCK, RocK or any other variation).
//3.Important note: you want to return the results of this function call, not console.log() them.
//Youre going to use what you return later on,
//so lets test this function by using console.log to see the results:
//4.Write a NEW function called game().
//Call the playRound function inside of this one to play a 5 round game
//that keeps score and reports a winner or loser at the end.
//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.
function getComputerChoice(){
const number = Math.floor(Math.random() * 3);
let computer = number;
if (number === 0){
computer = "rock";
}
else if (number === 1){
computer= "paper";
}
else{
computer= "scissors";
}
return computer;
}
function playRound(playerSelection, computerSelection){
let result = "It's a Tie!";
playerSelection = playerSelection.toLowerCase();
if (playerSelection === "rock" && computerSelection == "paper"){
result = "You Lose! Paper beats Rock";
console.log(playerSelection, computerSelection, result);
return false;
}
else if(playerSelection === "paper" && computerSelection == "rock"){
result = "You Win! Paper beats Rock";
console.log(playerSelection, computerSelection, result);
return true;
}
else if(playerSelection === "paper" && computerSelection == "scissors"){
result = "You Lose! Scissors beats Paper";
console.log(playerSelection, computerSelection, result);
return false;
}
else if(playerSelection === "scissors" && computerSelection == "paper"){
result = "You Win! Scissors beats Paper";
console.log(playerSelection, computerSelection, result);
return true;
}
else if(playerSelection === "rock" && computerSelection == "scissors"){
result = "You Win! Rock beats Scissors";
console.log(playerSelection, computerSelection, result);
return true;
}
else if(playerSelection === "scissors" && computerSelection == "rock"){
result = "You Lose! Rock beats Scissors";
console.log(playerSelection, computerSelection, result);
return false;
}
else{
console.log(playerSelection, computerSelection,result);
return null;
}
}
// console.log(playRound("Paper", getComputerChoice()));
function game(){
let player = 0;
let computer = 0;
let win = "You win";
while (player || computer < 5) {
let party = playRound(prompt("Rock, Paper or Scissors?"), getComputerChoice());
if(party === true){
player +=1;
}
else if(party === false){
computer +=1;
}
console.log(`Player score: ${player}/5, Computer score: ${computer}/5`);
if(player === 5){
return win = "You win!";
}
else if(computer === 5){
return win = "Computer wins!";
}
}
}
console.log(game());