Hi guys,
I tried the rock paper scissors project on javascript and run into “TypeError: getComputerChoice is not a function” when I tried to use a previously defined function as a parameter in another function. For some reason JS refuses to acknowledge that the function was previously defined. Can someone assist?
Code as follow:
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock'|| userInput === 'paper' || userInput === 'scissors') {
return userInput;
} else {
return console.log("Error");
}
}
const getComputerChoice = () => {
if (Math.floor(Math.random()*3) === 0) {
return 'rock';
} else if (Math.floor(Math.random()*3) === 1) {
return 'paper';
} else {
return 'scissors';
}
}
function determineWinner(getUserChoice,getComputerChoice) {
if (getUserChoice === getComputerChoice) {
console.log(getComputerChoice() + "tie")
} else if (getUserChoice === "rock") {
if (getComputerChoice === "paper") {
console.log(getComputerChoice(), "lose");
} else {
console.log(getComputerChoice(), "win");
}
} else if (getUserChoice === "paper") {
if (getComputerChoice === "rock") {
console.log(getComputerChoice(), "win");
} else {
console.log(getComputerChoice(), "lose");
}
} else if (getUserChoice === "scissors") {
if (getComputerChoice === "paper") {
console.log(getComputerChoice(), "win");
} else {
console.log(getComputerChoice(), "lose");
}
} else {
console.log(getComputerChoice(), "error");
}
}
console.log(determineWinner())
There quite a lot of problems with this code. But lets focus on the error at hand, and see how you can go from there.
so here, you declare a function with two parametes:
function determineWinner(getUserChoice,getComputerChoice)
yet when you call the function:
console.log(determineWinner())
you provide no arguments for the parameters, so the values of the parameters are now undefined
as we can see by adding a .log()
:
function determineWinner(getUserChoice,getComputerChoice) {
console.log(getUserChoice, getComputerChoice);
given both these variable are now undefined, this condition is true:
if (getUserChoice === getComputerChoice)
then here:
console.log(getComputerChoice() + "tie")
trying to call undefined as a function will result in an error
Thanks for the reply, helped alot. I fixed the code with a workaround by using a variable as a parameter instead of functions. However, I’m still curious if it’s possible for a function to take other functions as parameters and if possible how is it done?
Work around as follow:
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock'|| userInput === 'paper' || userInput === 'scissors') {
return userInput;
} else {
return console.log("Error");
}
}
const getComputerChoice = () => {
if (Math.floor(Math.random()*3) === 0) {
return 'rock';
} else if (Math.floor(Math.random()*3) === 1) {
return 'paper';
} else {
return 'scissors';
}
}
function determineWinner(userChoice) {
userChoice = getUserChoice(userChoice);
computerChoice = getComputerChoice();
if (userChoice === computerChoice) {
return computerChoice + " tie"
} else if (userChoice === "rock") {
if (computerChoice === "paper") {
return computerChoice + " lose";
} else {
return computerChoice + " win";
}
} else if (userChoice === "paper") {
if (computerChoice === "rock") {
return computerChoice + " win";
} else {
return computerChoice + " lose";
}
} else if (userChoice === "scissors") {
if (computerChoice === "paper") {
return computerChoice + " win";
} else {
return computerChoice + " lose";
}
} else {
return computerChoice + " error";
}
}
console.log(determineWinner("scissors"))
I would personally keep the two parameters of the determineWinner
function.
and do:
userChoice = getUserChoice(userChoice);
computerChoice = getComputerChoice();
console.log(determineWinner(userChoice, computerChoice));
or even better, make a playGame
function which gets the user and computer choice and pass it down to determine winner function
we can:
const myFunc = () => {
console.log('hello world');
}
const example = func => {
func()
}
example(myFunc)