I get an 'undefined' with rock, paper, scissors game


#1

Here’s my code, and when I enter playGame();
The output is:

Elegiste: piedra
La computadora elige: tijeras
Ganaste
undefined

// User’s choice
var userInput = ‘Piedra’ || ‘Papel’ || ‘Tijeras’;
userInput = userInput.toLowerCase();

// getComputerChoice
function getComputerChoice() {
var randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return ‘piedra’;
break;
case 1:
return ‘papel’;
break;
case 2:
return ‘tijeras’;
break;
}
}

// determineWinner
function determineWinner(userInput, getComputerChoice) {
if (userInput === getComputerChoice) {
console.log(‘Es un empate’);
} else if (userInput == ‘piedra’ && getComputerChoice == ‘papel’) {
console.log(‘Perdiste’);
} else if (userInput == ‘piedra’ && getComputerChoice == ‘tijeras’) {
console.log(‘Ganaste’);
} else if (userInput == ‘papel’ && getComputerChoice == ‘piedra’) {
console.log(‘Ganaste’);
} else if (userInput == ‘papel’ && getComputerChoice == ‘tijeras’) {
console.log(‘Perdiste’);
} else if (userInput == ‘tijeras’ && getComputerChoice == ‘piedra’) {
console.log(‘Perdiste’);
} else if (userInput == ‘tijeras’ && getComputerChoice == ‘papel’) {
console.log(‘Ganaste’);
}
}

// playGame
function playGame() {
var userChoice = userInput;
var computerChoice = getComputerChoice();
console.log('Elegiste: ’ + userChoice);
console.log('La computadora elige: ’ + computerChoice)
console.log(determineWinner(userChoice, computerChoice));
}

playGame(‘piedra’);


#2

Since the function is logging, you don’t need to log the return (or lack of one).


#3

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