-1
let a = undefined;

function getComputerChoice(a) {
    let n = (Math.random() * 3)
    if (n <= 1){
    return "rock"
    
  } else if (n > 1 && n <= 2){
    return "paper"
    
  } else  {
    return "scissors"
  }

};

let b = undefined

function getPlayerChoice(b) {
  let c = prompt("Rock, Paper, or Scissors?").toLocaleLowerCase(); 
  if (c === "rock"){
    return ("rock")
  }
  else if (c === "paper"){
    return ("paper")
  }   
  else if (c === "scissors"){
    return ("scissors")
  }
  else  {
    alert ("Input is not valid!")
  }
};

let playerChoice = getPlayerChoice(b);
let computerChoice = getComputerChoice(a);
let computerScore = 1;
let playerScore = 1;

function checkWinner(computer, player, cscore, pscore) {
  if (computer === "rock" && player === "rock"){ 
  }
  else if (computer === "rock" && player === "paper"){
    pscore++;
  }
  else if (computer === "rock" && player === "scissors"){
    cscore++;
  }
  else if (computer === "paper" && player === "rock"){
    cscore++;
  }
  else if (computer === "paper" && player === "paper"){
  }
  else if (computer === "paper" && player === "scissors"){
    pscore++;
  }
  else if (computer === "scissors" && player === "rock"){
    pscore++;
  }
  else if (computer === "scissors" && player === "paper"){
    cscore++;
  }
  else if (computer === "scissors" && player === "scissors"){
  }
  else {
  }

};

function checkWin (computer, player) {
  if (player === 6){
    alert("You Win!")
    
  }
  else if (computer === 6){
    alert ("You Lose!")
  }
  else {
    
  }
};

do {
  getPlayerChoice(b);
  getComputerChoice(a);
  checkWinner(computerChoice, playerChoice, computerScore, playerScore);
  checkWin(computerScore, playerScore);
  
} while (computerScore <=6 && playerScore <= 6);



So far my playerChoice and computerChoice are recording properly, and after adjusting the output of parts of the checkWinner function, it seems to be looping properly in the do {}. This leads me to believe the part in checkWinner that is meant to increment (cscore++) isn't working properly and I haven't been able to figure out why. It is possible it is incrementing properly but I haven't been able to see that happening with the debugger. I used starting number 1 and ending number 6 as I was worried 0 was returning null somewhere in the code and that was the issue.

  • Tried your code, you are getting, an input from user using a prompt, I think it is better to create a button with rock, paper, and scissors icon on it and get that value that is clicked. Also, you have nesting else ifs, do a switch(cleaner and easier to maintain), and last, computer === "rock" && player === "rock" better to computer == player, you will save 6 lines of your code. – Aaron Magpantay Dec 13 '22 at 23:42

1 Answers1

0

When you pass computerChoice, playerChoice, computerScore, and playerScore to checkWinner(), JavaScript passes the values of those variables to the function. Incrementing them in the function just modifies the local copies -- cscore and pscore -- not values in the original variables.

ivanatias
  • 3,105
  • 2
  • 14
  • 25
padeso
  • 390
  • 1
  • 8
  • That makes sense, and I think I had thought about that and later forgot. Although i'm not sure how to increment and change a global variables value within a function. – nodex99 Dec 14 '22 at 02:36
  • You can put the variables in an object and pass that instead: https://stackoverflow.com/questions/7744611/pass-variables-by-reference-in-javascript – padeso Dec 14 '22 at 10:34