function getComputerChoice(randomLetterGen) {
function randomLetterGen() {
function randomNumGen() {
return Math.floor(Math.random() * 3);
}
let word = "rps";
return word.charAt(randomNumGen());
}
let cpuInput;
if (randomLetterGen() == "r") {
cpuInput = "rock";
} else if (randomLetterGen() == "p") {
cpuInput = "paper";
} else if (randomLetterGen() == "s") {
cpuInput = "scissors";
}
return cpuInput;
}
console.log(getComputerChoice());
Question posted in Javascript
A very good W3school tutorial can be found here.
A very good W3school tutorial can be found here.
2
Answers
Each time you call randomLetterGen(), it generates a new random letter independently.
So, when you call it multiple times within the if statement, you’re potentially getting different results each time, leading to the possibility of getting undefined when none of the conditions match.
Also, you’re not seeing the undefined option when Math.random()*3 is less than 2, it’s because the randomLetterGen() function is likely returning either "r", "p", or "s", which means one of the if conditions will match and you’ll get a valid result.
You should call randomLetterGen() only one time and store it into a variable
also the getComputerChoice function expect to receive one parameter but you pass nothing into in. And the parameter name should not the same with the local variable
randomLetterGen