Learn javascript: functions


#1

Please help, determineWinner function does not work.

const getUserChoice = userInput => {
if (userInput === “rock” || userInput === “paper” || userInput === “scissors”) {
return userInput;
} else {
console.log(“Error message”);
}
};
console.log(getUserChoice(“rock”));

const getComputerChoice=()=> {
randomNumber= Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return “rock”;
case 1:
return “paper”;
case 2:
return “scissors”
}
}
console.log(getComputerChoice());

const determineWinner=function(getUserChoice, getComputerChoice){
if (getUserChoice === getComputerChoice){
return “Tie game”;
}
};


#2

Is this an exercise? Or a practice session?

Parameters to a function should not be function names. It will take some testing to be sure but I don’t believe function names are shadowed by local names. Use plain, local variables for the parameters.

 function(userChoice, computerChoice)

The call to the function may contain calls to the other two…

console.log(determineWinner(getUserChoice(), getComputerChoice());

#3

Thank you for your answer.

It is a project. Rock, Paper, Scissors

I have to compare 2 functions. Last function does not work.


#4

I found a problem.
It works. Yes, yes, yes…

const determineWinner= (getUserChoice, getComputerChoice)=>{
if (getUserChoice === getComputerChoice){
return “Tie game”;
}
}
console.log(determineWinner());


#5

The userChoice function is to take a parameter, userInput, so,

console.log(determineWinner(getUserChoice('rock'), getComputerChoice());

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.