I’m new in programming, and I’m doing the Odin Project. I’m having problems with the rock, paper, scissors exercise, where I need to compare the user choice and the machine choice. It’s the play()
function.
I already try to find the problem, and even look at the code of others (just in that part! not the whole exercise) but I can’t figure out where the problem is.
It just keep returning me random values.
Here is the code:
let userChoice = prompt("Choose your weapon: rock, paper or scissors").toLowerCase();
console.log(userChoice);
function getMachineChoice() {
let values = ["rock", "paper", "scissors"];
let randomValue = values[Math.floor(Math.random() * values.length)];
return randomValue;
}
console.log(getMachineChoice());
let machineChoice = getMachineChoice();
function play(userChoice, machineChoice) {
if (userChoice === machineChoice) {
return "Its a tie!";
} else if (userChoice === "rock" && machineChoice === "scissors") {
return "You win!";
} else if (userChoice === "paper" && machineChoice === "rock") {
return "You win!";
} else if (userChoice === "scissors" && machineChoice === "paper") {
return "You win!";
} else {
return "You lose!";
}
}
console.log(play(userChoice, machineChoice));