I am building the rock, paper scissors task. At the minute my code only works for 1 round. I'm unsure about how I would get this to keep the score, whilst repeating for 5 rounds. I'm under the impression i will need a for loop at least for the rounds, along the lines of:
for(i=0; i<5;i++);
but i don't know where to slot it into my code. I've looked around online, and i cant find a simple enough to understand resource that doesn't start using switch methods or any other more advanced code to build the game. Any help would be appreciated. Thanks.
function puterPlay() {
let random = Math.random();
if (random <= 0.3333) {
return "paper";
} else if (random >= 0.6666) {
return "rock";
} else {
return "scissors";
}
}
function playRound(playerSelection, puterSelection) {
if (playerSelection.toLowerCase() === "rock") {
if (puterSelection === "paper") {
puterScore++;
return lose;
} else if (puterSelection === "rock") {
return tie;
} else {
userScore++;
return win;
}
}
if (playerSelection.toLowerCase() === "scissors") {
if (puterSelection === "paper") {
userScore++;
return win;
} else if (puterSelection === "rock") {
puterScore++;
return lose;
} else {
return tie;
}
}
if (playerSelection.toLowerCase() === "paper") {
if (puterSelection === "paper") {
return tie;
} else if (puterSelection === "rock") {
userScore++;
return win;
} else {
puterScore++;
return lose;
}
}
}
let userScore = parseInt(0);
let puterScore = parseInt(0);
let win = "You win"
let lose = "You lose"
let tie = "It is a tie"
let playerSelection = prompt("Pick a move");
const puterSelection = puterPlay()
console.log(playRound(playerSelection, puterSelection))
console.log("your score = " + userScore);
console.log("Computer's score = " + puterScore);
I am building the rock, paper scissors task. At the minute my code only works for 1 round. I'm unsure about how I would get this to keep the score, whilst repeating for 5 rounds. I'm under the impression i will need a for loop at least for the rounds, along the lines of:
for(i=0; i<5;i++);
but i don't know where to slot it into my code. I've looked around online, and i cant find a simple enough to understand resource that doesn't start using switch methods or any other more advanced code to build the game. Any help would be appreciated. Thanks.
function puterPlay() {
let random = Math.random();
if (random <= 0.3333) {
return "paper";
} else if (random >= 0.6666) {
return "rock";
} else {
return "scissors";
}
}
function playRound(playerSelection, puterSelection) {
if (playerSelection.toLowerCase() === "rock") {
if (puterSelection === "paper") {
puterScore++;
return lose;
} else if (puterSelection === "rock") {
return tie;
} else {
userScore++;
return win;
}
}
if (playerSelection.toLowerCase() === "scissors") {
if (puterSelection === "paper") {
userScore++;
return win;
} else if (puterSelection === "rock") {
puterScore++;
return lose;
} else {
return tie;
}
}
if (playerSelection.toLowerCase() === "paper") {
if (puterSelection === "paper") {
return tie;
} else if (puterSelection === "rock") {
userScore++;
return win;
} else {
puterScore++;
return lose;
}
}
}
let userScore = parseInt(0);
let puterScore = parseInt(0);
let win = "You win"
let lose = "You lose"
let tie = "It is a tie"
let playerSelection = prompt("Pick a move");
const puterSelection = puterPlay()
console.log(playRound(playerSelection, puterSelection))
console.log("your score = " + userScore);
console.log("Computer's score = " + puterScore);
Share
edited Sep 19, 2018 at 11:16
Harshal Yeole
5,0011 gold badge23 silver badges43 bronze badges
asked Sep 19, 2018 at 11:03
user10325073user10325073
5
- you should check out arrays, so you can store the result every time you loop. Have you looked into that? – WiseStrawberry Commented Sep 19, 2018 at 11:05
- I haven't. I'm working through the Odin Project guide and arrays are covered after this game. I don't really want to veer off from their teaching order. Thanks though. – user10325073 Commented Sep 19, 2018 at 11:08
- Please mention ‘no arrays’ in your question before someone wastes their time on an answer. – Steven Spungin Commented Sep 19, 2018 at 11:10
- This has an example that you might want to follow: stackoverflow./questions/1527803/… – karen Commented Sep 19, 2018 at 11:11
- Added some updates to your code, check out the easier way with recursion. – Harshal Yeole Commented Sep 19, 2018 at 11:16
2 Answers
Reset to default 4I have edited your code snipet little bit hope it will fulfill your need :)
just put below code into the for loop
let playerSelection = prompt("Pick a move");
const puterSelection = puterPlay()
console.log(playRound(playerSelection, puterSelection))
console.log("your score = " + userScore);
console.log("Computer's score = " + puterScore);
function puterPlay() {
let random = Math.random();
if (random <= 0.3333) {
return "paper";
} else if (random >= 0.6666) {
return "rock";
} else {
return "scissors";
}
}
function playRound(playerSelection, puterSelection) {
if (playerSelection.toLowerCase() === "rock") {
if (puterSelection === "paper") {
puterScore++;
return lose;
} else if (puterSelection === "rock") {
return tie;
} else {
userScore++;
return win;
}
}
if (playerSelection.toLowerCase() === "scissors") {
if (puterSelection === "paper") {
userScore++;
return win;
} else if (puterSelection === "rock") {
puterScore++;
return lose;
} else {
return tie;
}
}
if (playerSelection.toLowerCase() === "paper") {
if (puterSelection === "paper") {
return tie;
} else if (puterSelection === "rock") {
userScore++;
return win;
} else {
puterScore++;
return lose;
}
}
}
let userScore = parseInt(0);
let puterScore = parseInt(0);
let win = "You win"
let lose = "You lose"
let tie = "It is a tie"
for(var i=0;i<5;i++){
let playerSelection = prompt("Pick a move");
const puterSelection = puterPlay()
console.log(playRound(playerSelection, puterSelection))
console.log("your score = " + userScore);
console.log("Computer's score = " + puterScore);
}
Try below code:
Looping is not a good approach, read here:
- Recursion vs loops
- https://www.refactoring./catalog/replaceIterationWithRecursion.html
It allows the user to play for 5 times.
Using recursion:
function puterPlay() {
let random = Math.random();
if (random <= 0.3333) {
return "paper";
} else if (random >= 0.6666) {
return "rock";
} else {
return "scissors";
}
}
function playRound(playerSelection, puterSelection) {
if (playerSelection.toLowerCase() === "rock") {
if (puterSelection === "paper") {
puterScore++;
return lose;
} else if (puterSelection === "rock") {
return tie;
} else {
userScore++;
return win;
}
}
if (playerSelection.toLowerCase() === "scissors") {
if (puterSelection === "paper") {
userScore++;
return win;
} else if (puterSelection === "rock") {
puterScore++;
return lose;
} else {
return tie;
}
}
if (playerSelection.toLowerCase() === "paper") {
if (puterSelection === "paper") {
return tie;
} else if (puterSelection === "rock") {
userScore++;
return win;
} else {
puterScore++;
return lose;
}
}
}
let userScore = parseInt(0);
let puterScore = parseInt(0);
let win = "You win"
let lose = "You lose"
let tie = "It is a tie"
var i = 0;
const play = () => {
let playerSelection = prompt("Pick a move");
const puterSelection = puterPlay()
console.log(playRound(playerSelection, puterSelection))
console.log("your score = " + userScore);
console.log("Computer's score = " + puterScore);
i++;
if (i !== 5) {
play();
} else {
alert("Game Over=> User("+userScore+") vs Computer("+puterScore+")");
}
}
play();