strong text
Hi, I can’t understand why call function in console.log I not getting ‘rock’, ‘paper’ or scissors?
strong text
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.
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())
Hi there, Thanks so much for clear explanation. I hope one day I be so good in Javascript as you
Hi there, Thanks for your replay.