0

I am working on a Rock/Paper/Scissors game like as usual. Somehow my if and switch statements cannot read the values set by the other two functions and when I console.log result it returns undefined

However when I manually set user and computer variables decideWinner function works as intended.

I've been tinkering with it for a couple of hours and some help would be greatly appreciated. Thanks.

let user
let computer
let result
//1
function userChoice() {
  let user = prompt(`rock, paper, scissors`)
}

//2
function computersTurn() {
  let randomNumber = Math.floor((Math.random() * 3) + 1);
  switch (randomNumber) {
    case 1:
      computer = `rock`;
      break;
    case 2:
      computer = `scissors`;
      break;
    case 3:
      computer = `paper`;
      break;
  }
}

//3
function decideWinner() {
  if (user == `rock`) {
    switch (computer) {
      case `rock`:
        result = `tie`
        break;
      case 'scissors':
        result = `player`
        break;
      case `paper`:
        result = `computer`
        break;
    }
  } else if (user == `paper`) {
    switch (computer) {
      case `rock`:
        result = `player`
        break;
      case 'scissors':
        result = `computer`
        break;
      case `paper`:
        result = `tie`
        break;
    }
  } else if (user == `scissors`) {
    switch (computer) {
      case `rock`:
        result = `computer`
        break;
      case 'scissors':
        result = `tie`
        break;
      case `paper`:
        result = `player`
        break;
    }
  }
}

userChoice();
computersTurn();
decideWinner();
console.log(result);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
armn
  • 7
  • 1
  • 1
    update the `userChoice()` method, remove the `let` keyword – AHMED SAJJAD Feb 19 '21 at 11:50
  • Take a look at [return](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return). Rather than changing global variables within your functions (which isn't usually a good idea as it can lead to hard to debug code), you can return the values from your function that you want to set your variables equal to – Nick Parsons Feb 19 '21 at 12:00

1 Answers1

1

You should not recreate the user variable. When you do this, another user variable is created locally and the prompt result is assigned to that variable, hence keeping your global user variable undefined. Use user = ... instead of let user = ...:

let user
let computer
let result
//1
function userChoice() {
  user = prompt(`rock, paper, scissors`)
}

//2
function computersTurn() {
  let randomNumber = Math.floor((Math.random() * 3) + 1);
  switch (randomNumber) {
    case 1:
      computer = `rock`;
      break;
    case 2:
      computer = `scissors`;
      break;
    case 3:
      computer = `paper`;
      break;
  }
}

//3
function decideWinner() {
  if (user == `rock`) {
    switch (computer) {
      case `rock`:
        result = `tie`
        break;
      case 'scissors':
        result = `player`
        break;
      case `paper`:
        result = `computer`
        break;
    }
  } else if (user == `paper`) {
    switch (computer) {
      case `rock`:
        result = `player`
        break;
      case 'scissors':
        result = `computer`
        break;
      case `paper`:
        result = `tie`
        break;
    }
  } else if (user == `scissors`) {
    switch (computer) {
      case `rock`:
        result = `computer`
        break;
      case 'scissors':
        result = `tie`
        break;
      case `paper`:
        result = `player`
        break;
    }
  }
}

userChoice();
computersTurn();
decideWinner();
console.log(result);

Note: you can rewrite your computersTurn function in a simpler way:

function computersTurn() {
  let choices = ["rock", "scissors", "paper"];
  let randomNumber = Math.floor((Math.random() * 3));
  computer = choices[randomNumber];
}
Wais Kamal
  • 5,858
  • 2
  • 17
  • 36