1

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 computerPlay() {
  let random = Math.random();
  if (random <= 0.3333) {
    return "paper";
  } else if (random >= 0.6666) {
    return "rock";
  } else {
    return "scissors";
  }
}


function playRound(playerSelection, computerSelection) {
  if (playerSelection.toLowerCase() === "rock") {
    if (computerSelection === "paper") {
      computerScore++;
      return lose;
    } else if (computerSelection === "rock") {
      return tie;
    } else {
      userScore++;
      return win;
    }
  }

  if (playerSelection.toLowerCase() === "scissors") {
    if (computerSelection === "paper") {
      userScore++;
      return win;
    } else if (computerSelection === "rock") {
      computerScore++;
      return lose;
    } else {
      return tie;
    }
  }

  if (playerSelection.toLowerCase() === "paper") {
    if (computerSelection === "paper") {
      return tie;
    } else if (computerSelection === "rock") {
      userScore++;
      return win;
    } else {
      computerScore++;
      return lose;
    }
  }
}


let userScore = parseInt(0);
let computerScore = parseInt(0);
let win = "You win"
let lose = "You lose"
let tie = "It is a tie"
let playerSelection = prompt("Pick a move");
const computerSelection = computerPlay()
console.log(playRound(playerSelection, computerSelection))
console.log("your score = " + userScore);
console.log("Computer's score = " + computerScore);
Harshal Yeole
  • 4,812
  • 1
  • 21
  • 43
  • you should check out arrays, so you can store the result every time you loop. Have you looked into that? – WiseStrawberry Sep 19 '18 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. –  Sep 19 '18 at 11:08
  • Please mention ‘no arrays’ in your question before someone wastes their time on an answer. – Steven Spungin Sep 19 '18 at 11:10
  • This has an example that you might want to follow: https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range – karen Sep 19 '18 at 11:11
  • Added some updates to your code, check out the easier way with recursion. – Harshal Yeole Sep 19 '18 at 11:16

2 Answers2

4

I 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 computerSelection = computerPlay()
console.log(playRound(playerSelection, computerSelection))
console.log("your score = " + userScore);
console.log("Computer's score = " + computerScore);

function computerPlay() {
  let random = Math.random();
  if (random <= 0.3333) {
    return "paper";
  } else if (random >= 0.6666) {
    return "rock";
  } else {
    return "scissors";
  }
}


function playRound(playerSelection, computerSelection) {
  if (playerSelection.toLowerCase() === "rock") {
    if (computerSelection === "paper") {
      computerScore++;
      return lose;
    } else if (computerSelection === "rock") {
      return tie;
    } else {
      userScore++;
      return win;
    }
  }

  if (playerSelection.toLowerCase() === "scissors") {
    if (computerSelection === "paper") {
      userScore++;
      return win;
    } else if (computerSelection === "rock") {
      computerScore++;
      return lose;
    } else {
      return tie;
    }
  }

  if (playerSelection.toLowerCase() === "paper") {
    if (computerSelection === "paper") {
      return tie;
    } else if (computerSelection === "rock") {
      userScore++;
      return win;
    } else {
      computerScore++;
      return lose;
    }
  }
}


let userScore = parseInt(0);
let computerScore = 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 computerSelection = computerPlay()
  console.log(playRound(playerSelection, computerSelection))
  console.log("your score = " + userScore);
  console.log("Computer's score = " + computerScore);
}
Dhaval Pankhaniya
  • 1,996
  • 1
  • 15
  • 26
3

Try below code:

Looping is not a good approach, read here:

It allows the user to play for 5 times.

Using recursion:

function computerPlay() {
    let random = Math.random();
    if (random <= 0.3333) {
        return "paper";
    } else if (random >= 0.6666) {
        return "rock";
    } else {
        return "scissors";
    }
}


function playRound(playerSelection, computerSelection) {
    if (playerSelection.toLowerCase() === "rock") {
        if (computerSelection === "paper") {
            computerScore++;
            return lose;
        } else if (computerSelection === "rock") {
            return tie;
        } else {
            userScore++;
            return win;
        }
    }

    if (playerSelection.toLowerCase() === "scissors") {
        if (computerSelection === "paper") {
            userScore++;
            return win;
        } else if (computerSelection === "rock") {
            computerScore++;
            return lose;
        } else {
            return tie;
        }
    }

    if (playerSelection.toLowerCase() === "paper") {
        if (computerSelection === "paper") {
            return tie;
        } else if (computerSelection === "rock") {
            userScore++;
            return win;
        } else {
            computerScore++;
            return lose;
        }
    }
}


let userScore = parseInt(0);
let computerScore = 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 computerSelection = computerPlay()
    console.log(playRound(playerSelection, computerSelection))
    console.log("your score = " + userScore);
    console.log("Computer's score = " + computerScore);
    i++;
    if (i !== 5) {
        play();
    } else {
        alert("Game Over=> User("+userScore+") vs Computer("+computerScore+")");
    }
}

play();
Harshal Yeole
  • 4,812
  • 1
  • 21
  • 43
  • [this will make things more clear about loops and recursion :)](https://cs.stackexchange.com/questions/56867/why-are-loops-faster-than-recursion) – Dhaval Pankhaniya Sep 19 '18 at 11:29