dimanche 28 mars 2021

Rock Paper Scissor Javascript only lets me tie, no matter what I input when invoking the function. I don't understand where I went wrong?

First off: I am sorry for another rock paper scissors JS question.

I do not think I made any errors with syntax and honestly do not comprehend why it always returns "tie" three times in a row, stating that the computer chose the same choice, three times in a row. If I re-run the code it chooses a different choice, but it still repeats three times, despite the user input in the function evocations being different in all three...

const computer = Math.random()

function game(user, computer){
  if (user = "paper" && computer > .66) {
    return console.log("computer chose paper! it's a tie");
  }
  else if (user = "rock" && computer <= .33 && computer <= .66) {
    return console.log("computer chose rock! it's a tie");
  }
  else if (user="scissors" && computer >= .33) {
    return console.log("computer chose scissors! it's a tie");
  }
  /* you lose */
  else if (user = "paper" && computer >= .33) { /*comp scissor */
    return console.log("computer chose scissors! you lose!");
  }
  else if (user = "scissors" && computer <= .33 && computer <= .66) { /* comp rock */
    return console.log("computer chose rock! you lose!");
  }
  else if (user = "rock" && computer > .66) { /* comp paper */
    return console.log("computer chose paper! you lose!");
  }
  /* you win */
  else if (user = "scissors" && computer > .66) { /* comp paper*/
    return console.log("computer chose paper! you win!");
  }
  else if (user = "rock" && computer >= .33) { /* comp scissors */
    return console.log("computer chose scissors! you win!");
  }
  else if (user = "paper" &&  computer <= .33 && computer <= .66) { /*rock */
    return console.log("computer chose rock! you win!");
  }
}

game("paper", computer);
game("rock", computer);
game("scissors", computer);

I know that there are definitely more efficient and different ways of creating this game, but I still want to understand what is wrong with what I did here, and the logic behind it... Thanks in advance. Have a good day.

Aucun commentaire:

Enregistrer un commentaire