Rock,paper,scissors

strong text

const getUserChoice = userInput => { userInput = userInput.toLowerCase(); if (userInput === 'rock'|| userInput === 'paper' || userInput ==='scissors') { return userInput; } else { console.log('Error!'); } } const getComputerChoice = () => { let randomNumber = Math.floor(Math.random() * 3); switch (randomNumber) { case 0: return 'rock'; break; case 1: return 'paper'; break; case 2: return 'scissors' break; console.log(getComputerChoice()); } };

Hi, I can’t understand why call function in console.log I not getting ‘rock’, ‘paper’ or scissors?

Hi, there.

You currently have

console.log(getComputerChoice());

Within the getComputerChoice() method. You want to log that outside of the method.

1 Like

You are trying to log the computer’s choice by calling the getComputerChoice() function inside of itself. This leads to an infinite loop because every time the getComputerChoice function is run it gets to the console.log() line and tries to log the result of calling itself.

Technically, though, it never reaches this point because right above it, as part of the switch case where userChoice === 2, you have a ‘break’ which exits the function. Above that, you also return the appropriate strings for the results of the various cases which also exits the function. So you can get rid of your break’s and also, as kira mentioned, you’ll have to put the console.log() outside of the function body.

As in,

const getComputerChoice = () => {
  const computerChoice = ...
  switch (computerChoice) {
    case 0:
      return 'rock'
    ...
  }
}

console.log(getComputerChoice())
2 Likes

Hi there, Thanks so much for clear explanation. I hope one day I be so good in Javascript as you :grinning:

Hi there, Thanks for your replay.