-1

I'm creating a Scissors Paper rock game with functions and if statements. I am getting a bug currently which returns only the DRAW case. Would appreciate if anyone else can spot the bug.

the link to my code is here: https://github.com/BladeMountain/SissorPaperRock/blob/master/SPR_script.html

function computerPlay() {
  let choice = ['rock', 'paper', 'scissor'];
  let randomChoice = [Math.floor(Math.random() * choice.length)];
  return randomChoice;
}

function playGame(playerSelection, computerSelection) {
  if (playerSelection == "scissor" && computerSelection == "paper") {
    return "You Won";
  } else if (playerSelection == "paper" && computerSelection == "rock") {
    return "You Won";
  } else if (playerSelection == "rock" && computerSelection == "scissor") {
    return "You Won";
  } else if (playerSelection == "scissor" && computerSelection == "rock") {
    return "You Lost";
  } else if (playerSelection == "paper" && computerSelection == "scissor") {
    return "You Lost";
  } else if (playerSelection == "rock" && computerSelection == "paper") {
    return "You Lost";
  } else if (playerSelection == "rock" && computerSelection == "rock") {
    return "Draw";
  } else if (playerSelection == "scissor" && computerSelection == "scissor") {
    return "Draw";
  } else if (playerSelection == "paper" && computerSelection == "paper") {
    return "Draw";
  } else {
    return 'Invalid response, this is case insensitive. Please input either scissor, paper or rock';
  }
}

function game() {
  let roundOne = playGame(playerSelection, computerSelection);
  let roundTwo = playGame(playerSelection, computerSelection);
  let roundThree = playGame(playerSelection, computerSelection);
  let roundFour = playGame(playerSelection, computerSelection);
  let roundFive = playGame(playerSelection, computerSelection);

  console.log('Round 1 = ' + roundOne);
  console.log('Round 2 = ' + roundTwo);
  console.log('Round 3 = ' + roundThree);
  console.log('Round 4 = ' + roundFour);
  console.log('Round 5 = ' + roundFive);

}

let playerSelection = prompt("Please select one", "scissor/paper/rock");
let computerSelection = computerPlay();
console.log(playGame(playerSelection, computerSelection));

// this is only still executing my else statement. Anything that I need to change?

Also, I am new to StackOverflow and coding, hopefully, it's not a stupid question.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • 2
    please add the code to the question. what error do you get? – Nina Scholz May 06 '20 at 07:51
  • 1
    The draw could be easier checked with `playerSelection == computerSelection`. – Lain May 06 '20 at 07:56
  • You're returning the wrong value for `computerSelection`, you want `choice[Math.floor(Math.random() * choice.length)]` – Phil May 06 '20 at 07:57
  • Duplicate of [Getting a random value from a JavaScript array](https://stackoverflow.com/questions/4550505/getting-a-random-value-from-a-javascript-array) I suppose but I already voted to close as a typo – Phil May 06 '20 at 07:59
  • I suggest you get to know your browser's JavaScript debugger. It will make solving these sorts of problems trivial ~ https://developers.google.com/web/tools/chrome-devtools/javascript – Phil May 06 '20 at 08:00

1 Answers1

-1

Please check code below.... i made change in function computerPlay()

instead of returning random number return array value... just one change in return of function computerPlay()

// this is only still executing my else statement. Anything that I need to change?

function computerPlay() {
let choice = ['rock', 'paper', 'scissor'];
let randomChoice = [Math.floor(Math.random()*choice.length)];
return choice[randomChoice];
}

function playGame(playerSelection, computerSelection) {
  if (playerSelection == "scissor" && computerSelection == "paper"){
      return "You Won";
  } else if (playerSelection == "paper" && computerSelection == "rock"){
      return "You Won";
  } else if (playerSelection == "rock" && computerSelection == "scissor"){
      return "You Won";
  } else if (playerSelection == "scissor" && computerSelection == "rock"){
      return "You Lost";
  } else if (playerSelection == "paper" && computerSelection == "scissor"){
      return "You Lost";
  } else if (playerSelection == "rock" && computerSelection == "paper"){
      return "You Lost";
  } else if (playerSelection == "rock" && computerSelection == "rock"){
      return "Draw";
  } else if (playerSelection == "scissor" && computerSelection == "scissor"){
      return "Draw";
  } else if (playerSelection == "paper" && computerSelection == "paper"){
      return "Draw";
  } else {
    return'Invalid response, this is case insensitive. Please input either scissor, paper or rock';
  }
}

function game() {
  let roundOne = playGame(playerSelection, computerSelection);
  let roundTwo = playGame(playerSelection, computerSelection);
  let roundThree = playGame(playerSelection, computerSelection);
  let roundFour = playGame(playerSelection, computerSelection);
  let roundFive = playGame(playerSelection, computerSelection);
  
  console.log('Round 1 = ' + roundOne);
  console.log('Round 2 = ' + roundTwo);
  console.log('Round 3 = ' + roundThree);
  console.log('Round 4 = ' + roundFour);
  console.log('Round 5 = ' + roundFive);
 
}

let playerSelection = prompt("Please select one", "scissor/paper/rock");
let computerSelection = computerPlay();
console.log(playGame(playerSelection, computerSelection));

// this is only still executing my else statement. Anything that I need to change?
<!DOC HTMl> 
<html>


</html>
Deepu
  • 198
  • 1
  • 8
  • 1
    Please do not answer typo-type questions (code with very little change to make it work and already answered in comments) – mplungjan May 06 '20 at 08:25