Help with Javascript Rock, Paper, Scissors

For the rock, paper, scissors project I’m receiving ‘undefined’ whenever I call my determineWinner function in playGame. It seems to work fine calling it by itself, anyone have any ideas on what’s causing the undefined?

const determineWinner = (userChoice, computerChoice) => {
    if(userChoice === computerChoice) {
        return 'The match was a tie.'; 
    }; 

    if(userChoice === 'bomb') {
        return 'YOU WIN!'
    }

    if(userChoice === 'rock') {
        if(computerChoice === 'paper') {
            return 'The computer won!';
        } else {
            return 'You win!'; 
        };
    };

    if(userChoice === 'paper') {
        if(computerChoice === 'scissors') {
            return 'The computer won!';
        } else {
            return 'You win!'; 
        };
    };

    if(userChoice === 'scissors') {
        if(computerChoice === 'rock') {
            return 'The computer won!';
        } else {
            return 'You win!'; 
        };
    };
};

const playGame = () => {
    let userChoice = getUserChoice('rock');
    console.log(userChoice);
    let computerChoice = getComputerChoice();
    console.log(computerChoice); 

    return console.log(determineWinner(userChoice, computerChoice)); 
}

// works here
console.log(determineWinner('bomb', 'paper')); 

// does not work here 
playGame();

because here:

return console.log(determineWinner(userChoice, computerChoice));

you use both log and return. That doesn’t work

.log() method doesn’t return anything, so that results in undefined, which you then return

3 Likes

yeah I got rid of the return and I’m still getting the same undefined. My console output’s:

rock
undefined
scissors
undefined

Why would you want to get rid of your return? And can I then see an update of your entire code?

I misunderstood what you meant in your other comment, but yeah here’s my code:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase(); 
  if(userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
    console.log(userInput); 
  } else {
    console.log('Error'); 
  }; 
};


const getComputerChoice = () => {
  let num = Math.floor(Math.random() * 3);
  switch(num) {
    case 0:
      return 'rock'; 
      break;
    case 1: 
      return 'paper'; 
      break; 
    case 2: 
      return 'scissors'; 
      break; 
  };   
}; 

const determineWinner = (userChoice, computerChoice) => {
    if(userChoice === computerChoice) {
        return 'The match was a tie.'; 
    }; 

    if(userChoice === 'bomb') {
        return 'YOU WIN!'
    }

    if(userChoice === 'rock') {
        if(computerChoice === 'paper') {
            return 'The computer won!';
        } else {
            return 'You win!'; 
        };
    };

    if(userChoice === 'paper') {
        if(computerChoice === 'scissors') {
            return 'The computer won!';
        } else {
            return 'You win!'; 
        };
    };

    if(userChoice === 'scissors') {
        if(computerChoice === 'rock') {
            return 'The computer won!';
        } else {
            return 'You win!'; 
        };
    };
};

const playGame = () => {
    let userChoice = getUserChoice('bomb');
    console.log(userChoice);
    let computerChoice = getComputerChoice();
    console.log(computerChoice); 

    return determineWinner(userChoice, computerChoice); 
}



playGame();

I am doing that on purpose, critical and analytical think are very important skills. If I just straight up to you: do this, you will learn less

Similar problem, here:

const playGame = () => {
    let userChoice = getUserChoice('bomb');
    console.log(userChoice);

getUserChoice doesn’t return antying. It only logs within the getUserChoice function itself