-1

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));
Biffen
  • 6,249
  • 6
  • 28
  • 36
Horus
  • 13
  • 1
  • In your getMachineChoice() you return randomValue, but you actually want "rock", "paper" or "scissors", no? :) – meskobalazs Apr 27 '23 at 12:04
  • 2
    "_It just keep returning me random values_" Yes, that's the purpose of `Math.random()`, it returns a random value _every time_ it's called. Can you please elaborate your question. – Teemu Apr 27 '23 at 12:04
  • 1
    This is a good chance to learn how to use a [debugger](https://stackoverflow.com/questions/25385173/), where you can step through the code statement by statement, look at the state of variables, and follow your logic through. More [here](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – T.J. Crowder Apr 27 '23 at 12:04
  • 1
    @meskobalazs - `randomValue` **is** one of those strings: `let randomValue = values[Math.floor(Math.random() * values.length)];` – T.J. Crowder Apr 27 '23 at 12:05
  • 3
    Note that what you're logging (`console.log(getMachineChoice());`) is not what you're putting in `machineChoice`, because you're calling `getMachineChoice()` again, and it's random. So what you're logging is misleading you. – T.J. Crowder Apr 27 '23 at 12:09

1 Answers1

1

What you need is to assign the result of getMachineChoice() into the variable machineChoice first before you call the console.log. With that you can get the correct user and machine choice in the console.

(Instead of calling getMachineChoice() twice, you should only call it once to generate the result once. Then, assign it to a variable which it can be used for comparison later)

let userChoice = prompt(
  'Choose your weapon: rock, paper or scissors'
).toLowerCase();
console.log(userChoice);

let machineChoice = getMachineChoice();
function getMachineChoice() {
  let values = ['rock', 'paper', 'scissors'];
  let randomValue = values[Math.floor(Math.random() * values.length)];
  return randomValue;
}
console.log(machineChoice);

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));
AngYC
  • 3,051
  • 6
  • 20