"Next Steps" Undefined? What does that mean?


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/5?curriculum_id=506324b3a7dffd00020bf661


Hello! This is my first post around and I hope you can help me. I am really begining in this world and I am truly sorry if this is very simple for you! It seems I am doing something wrong... Or maybe this is not the best way to solve the problem. When the game returns a tie the program runs normally (I think) but the word "undefined" shows up. Something like this, after the tie is resolved:

Computer: paper
paper wins
undefined


    var userChoice = prompt("Do you choose rock, paper or scissors?");
    var computerChoice = Math.random()
    var randomChoice = function(randomNumber) {
        if (computerChoice < 0.34) {
        	computerChoice = "rock";
        } else if(computerChoice <= 0.67) {
        	computerChoice = "paper";
        } else {
        	computerChoice = "scissors";
        } console.log("Computer: " + computerChoice);
    }
    randomChoice(computerChoice)
    
    
    
    var compare = function(choice1, choice2) {
        if(choice1 === choice2) {
            userChoice = prompt(" Tie! Please choose another user value:");
            computerChoice = Math.random();
            randomChoice(computerChoice);
        }
        
        else if (choice1 === "rock") {
            if (choice2 === "scissors") {
                return "rock wins";
            } else {
                return "paper wins";
            }
        }
        
        else if (choice1 === "paper") {
            if (choice2 === "scissors") {
                return "scissors wins";
            } else {
                return "paper wins";
            }
        }
        
        else if (choice1 === "scissors") {
            if (choice2 === "paper") {
                return "scissors wins";
            } else {
                return "rock wins";
            }
            
        } else {
            return "Your choice is not valid. Please try again";
        }
        console.log(compare(userChoice, computerChoice))
    }
    
    console.log(compare(userChoice, computerChoice))


#2

the problem is here:

console.log(compare(userChoice, computerChoice))

when you call the function again from inside the function. This function call doesn't return anything, causing a undefined, you can fix it by changing console.log() into return


#3

Well, thank you so much! It really solves the problem, but unfortunately I am not understanding the reason...

If I just call the function it doesn't work and it causes a "undefined" too. If I put the console.log it seems to work, because it does return the outcome of the game - for example "paper wins" - but it also shows the "undefined". And with the "return" instead of the "console.log" it works just fine without the "undefined". I don't want to bother you too much, but if you could help me to understand it I would be really grateful!


#4

if the result is a tie, this function call here:

} else {
            return "Your choice is not valid. Please try again";
        }
        console.log(compare(userChoice, computerChoice)) # here!
    }

gets called again, agree? lets say rock wins is returned (after a tie), which is nice, and console.log will log this result to the console, however, it doesn't return anything, which means the original function call (last line) doesn't get anything in return, logging a undefined


#5

Oh I think I finally got it... So, the original function (the first one called, last line) needs to return something, which doesn't happen if I just ask for a "console.log" after the tie, and that's the undefined thing.
Thank you so much for your time and help!!


#6

yea, the original function (last line) doesn't get anything returned (because the second function call only logs to the console) and doesn't return, so the original function doesn't get anything returned, logging a undefined.

no, the opposite, the original function doesn't need to return something, it needs to get something returned, otherwise it will log undefined to the console


#7

Yes that's it. I got it. It was just my english that betrayed me. Not a native :slight_smile:
Thank you once more!


#8

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