skip to Main Content

Im stuck in trying to create a round loop for the game. The first round works fine but can’t get past that. The farthest I got was to just show the first answer twice in a row, but that broke the game by giving random results also.

Here’s my code:

let randomvalue = Math.random();
let rock = "rock";
let paper = "paper";
let scissors = "scissors";

function getComputerChoice() {
  if (randomvalue <= 0.33) {
    return rock;
  } else if (randomvalue <= 0.66) {
    return paper;
  } else return scissors;
}

let playerscore = 0;
let computerscore = 0;

function results(player, computer) {
  if (player === rock && computer === rock) {
    return "Its a TIE!";
  } else if (player === rock && computer === paper) {
    computerscore++;
    return "You LOSE! Paper beats Rock.";
  } else if (player === rock && computer === scissors) {
    playerscore++;
    return "You WIN! Rock beats Scissors.";
  }

  if (player === scissors && computer === scissors) {
    return "Its a TIE!";
  } else if (player === scissors && computer === rock) {
    return "You LOSE! Rock beats Scissors.";
    computerscore++;
  } else if (player === scissors && computer === paper) {
    playerscore++;
    return "You WIN! Scissors beats Paper.";
  }

  if (player === paper && computer === paper) {
    return "Its a TIE!";
  } else if (player === paper && computer === scissors) {
    computerscore++;
    return "You LOSE! Scissors beats Paper.";
  } else if (player === paper && computer === rock) {
    playerscore++;
    return "You WIN! Paper beats Rock.";
  }
}

function playerchoice() {
  let choice = prompt("Whats your choice?(rock, paper, scissors)");
  if (choice === "rock") {
    return rock;
  }
  ese
  if (choice === "paper") {
    return paper;
  } else {
    return scissors;
  }
}

function WinGame() {
  if (playerscore === 5) {
    return "You WIN!";
  } else if (computerscore === 5) {
    return "You LOSE!";
  }
}

const computer = randomvalue;
alert(results(playerchoice(), getComputerChoice()));

alert("Player Score: " + playerscore);
alert("Computer Score: " + computerscore);

while ((playerscore < 5) && (computerscore < 5)) {
  prompt("Whats your choice?(rock, paper, scissors)")
}

I tried a while loop, alone and inserted in playerchoice function, but that just repeats the first screens in a row, not even showing the scores.

2

Answers


  1. There are a few issues with your code;

    randomvalue is declared only once and so getComputerChoice() will always return the same value in each round as its based on the same random number.

    playerchoice() function has a syntax error as ese should be else.

    In the while loop, your prompting the player to make a choice but your not using the results to compare with the computers choices – also your not updating the score based on the outcome of the hand.

    The WinGame() function is never called and it doesn’t interrupt the game once a player reaches 5 points.

    let rock = "rock";
    let paper = "paper";
    let scissors = "scissors";
    
    function getComputerChoice() {
      let randomvalue = Math.random();
      if (randomvalue <= 0.33) {
        return rock;
      } else if (randomvalue <= 0.66) {
        return paper;
      } else return scissors;
    }
    
    let playerscore = 0;
    let computerscore = 0;
    
    function results(player, computer) {
      if (player === rock && computer === rock) {
        return "It's a TIE!";
      } else if (player === rock && computer === paper) {
        computerscore++;
        return "You LOSE! Paper beats Rock.";
      } else if (player === rock && computer === scissors) {
        playerscore++;
        return "You WIN! Rock beats Scissors.";
      }
    
      if (player === scissors && computer === scissors) {
        return "It's a TIE!";
      } else if (player === scissors && computer === rock) {
        computerscore++;
        return "You LOSE! Rock beats Scissors.";
      } else if (player === scissors && computer === paper) {
        playerscore++;
        return "You WIN! Scissors beats Paper.";
      }
    
      if (player === paper && computer === paper) {
        return "It's a TIE!";
      } else if (player === paper && computer === scissors) {
        computerscore++;
        return "You LOSE! Scissors beats Paper.";
      } else if (player === paper && computer === rock) {
        playerscore++;
        return "You WIN! Paper beats Rock.";
      }
    }
    
    function playerchoice() {
      let choice = prompt("What's your choice?(rock, paper, scissors)");
      if (choice === "rock") {
        return rock;
      } else if (choice === "paper") {
        return paper;
      } else {
        return scissors;
      }
    }
    
    while ((playerscore < 5) && (computerscore < 5)) {
      let player = playerchoice();
      let computer = getComputerChoice();
      alert(results(player, computer));
      alert("Player Score: " + playerscore);
      alert("Computer Score: " + computerscore);
    
      if (playerscore === 5) {
        alert("You WIN!");
        break;
      } else if (computerscore === 5) {
        alert("You LOSE!");
        break;
      }
    }
    Login or Signup to reply.
  2. A few things here, I hope this helps you clear out your confusion!

    1st you need to move the random number fetching line inside the function call so it is changed every time the function is called

    function getComputerChoice() {
    let randomvalue = Math.random();
      if (randomvalue <= 0.33) {
        return rock;
      } else if (randomvalue <= 0.66) {
        return paper;
      } else return scissors;
    }
    

    second you need to call the get choice methods inside the loop to make sure they’re called each time!

    while ((playerscore < 5) && (computerscore < 5)) {
      alert(results(playerchoice(), getComputerChoice()));
      alert("Player Score: " + playerscore);
      alert("Computer Score: " + computerscore);
    }
    

    lastly calling the win game method

    alert(WinGame());
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search