Link: https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-i/modules/fecp-learn-javascript-syntax-functions/projects/rock-paper-scissors-javascript
Project: Rock, Paper, or Scissors
The outcome of my code works good but it shows undefined in the end. How to fix it?
const getUserChoice = function(userInput){
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' ) {
return userInput;
} else {
console.log('Error!');
}
}
const getComputerChoice = function(){
const comrandomguess = Math.floor(Math.random()*3)
if (comrandomguess === 0){
return 'rock'
} else if (comrandomguess === 1){
return 'paper'
} else if (comrandomguess ===2){
return 'scissors'
}
}
const determineWinner = function(userChoice, computerChoice){
if (userChoice === computerChoice){
return 'tie'
}
if (userChoice === 'rock'){
if(computerChoice === 'paper'){
return 'computer wins'
} else {
return 'user wins'}
}
if (userChoice === 'scissors'){
if(computerChoice === 'rock'){
return 'computer wins'
} else {
return 'user wins'}
}
if (userChoice === 'paper'){
if(computerChoice === 'scissors'){
return 'computer wins'
} else {
return 'user wins'}
}
}
const playGame = function(){
const userChoice = getUserChoice('paper');
const computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw:' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
console.log(playGame());
mirja_t
December 22, 2021, 9:38am
2
If you log a console.log
console.log(console.log('You threw: ' + userChoice))
the logged console returns ‘undefined’.
Instead of logging the string within your playGame function, return the string.
Thank you for the help. Problem solved
I am having the same issue, however I don’t understand your answer. Would you be able to explain it differently?
Do you have the revisions you made still? I am having the exact same problem, but I don’t understand the answer that was given.
function fn() {
console.log('hello'); // logs "hello" when called
}
function fn2() {
return 'hello';
}
console.log(fn()); // logs undefined
console.log(fn2()); // logs "hello"
Maybe this also helps: Don't understand why at the end it shows undefined
Thank you. As it turns out, my issue was similar, not the same.
I had console.log() written into the determineWinnner function. This would give me the info I needed with an additional line “undefined” printed in the console. when I called playGame. The function playGame was trying to console.log(console.log)) resulting in the undefined line.
I returned the lines in determineWinner instead of console.log() and now it works perfectly when I call playGame.
***note - I did not use console.log(playGame()) for the last line to log my results. I used playGame(). I think doing this would also fix the original issue
Hopefully this helps with anyone that is getting my issue.