const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
return userInput
} else console.log('Error, type properly')
}
function getComputerChoice() {
let num = Math.floor(Math.random() * 2)
if (num === 0){
return 'rock'
} else if (num === 1){
return 'paper'
} else if (num === 2){
return 'scissors'
}
}
function determineWinner(userChoice, computerChoice) {
if(userChoice === computerChoice){
return 'The game was a tie'
}
if(userChoice === 'rock'){
if(computerChoice === 'paper'){
return 'computer won'
} else{
return 'user won'
}
}
if(userChoice === 'paper'){
if(computerChoice === 'scissors'){
return 'computer won'
} else{
return 'user won'
}
}
if(userChoice === 'scissors'){
if(computerChoice === 'rock'){
return 'computer won'
} else{
return 'user won'
}
}
}
console.log(determineWinner(getUserChoice, getComputerChoice))
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if(userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’){
return userInput
} else console.log(‘Error, type properly’)
}
function getComputerChoice() {
let num = Math.floor(Math.random() * 2)
if (num === 0){
return ‘rock’
} else if (num === 1){
return ‘paper’
} else if (num === 2){
return ‘scissors’
}
}
function determineWinner(userChoice, computerChoice) {
if(userChoice === computerChoice){
return ‘The game was a tie’
}
if(userChoice === 'rock'){
if(computerChoice === 'paper'){
return 'computer won'
} else{
return ‘user won’
}
}
if(userChoice === 'paper'){
if(computerChoice === 'scissors'){
return 'computer won'
} else{
return 'user won'
}
}
if(userChoice === 'scissors'){
if(computerChoice === 'rock'){
return 'computer won'
} else{
return ‘user won’
}
}
}
console.log(determineWinner(getUserChoice, getComputerChoice))
this code is returning undefined
You have
console.log(determineWinner(getUserChoice, getComputerChoice))
but getUserChoice
and getComputerChoice
are functions that should be called there, not variables.
You can set up variables to get the result of calling those functions, and then use those resulting variables as arguments for [calling] determineWinner
const userChoice = getUserChoice("paper"); // getUserChoice needs one argument
const computerChoice = getComputerChoice(); // no arguments for getComputerChoice
console.log( determineWinner(userChoice, computerChoice) );
or you could do that on one line if you really want to:
console.log( determineWinner(getUserChoice("paper"), getComputerChoice() ) );
Notice that the result of the function call, not the function would be the argument there, so the ()
and the ("paper")
is necessary somewhere.
2 Likes