9.Just a misunderstood question


#1


Hi.below this is my final code which is correct...but i wonder how console prints the stuff i wrote after "return"...like "The result is a tie!" is written after "return", not in "console.log()"...so why does console print it out?


var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare = function(choice1,choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!"
    }else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins"} else {return "paper wins"}
    }else if (choice1 === "paper") {
        if(choice2 === "rock"){return "paper wins"}
            else {return "scissors wins"}
    }else if (choice1 === "scissors") {
        if(choice2 === "rock"){return "rock wins"}
            else {return "scissors wins"}
    }
}
compare (userChoice,computerChoice);


#2

Think of your var compare code as the instruction manual for your function. To get the function to display its execution to the console, you use console log and input the compare function with the parameters:

console.log(compare (userChoice, computerChoice));

Return statements in the function don't display to the console on their own. When you call the function, the return statements are still executing.


#3

so in this one
https://repl.it/DM6t
the green line isn't printed in browser,it just informs me...right?
and in this one
https://repl.it/DM63
the result is printed and the green line says "undefined"...so why is he writing "undefined"?

and in codeacademy native it still prints...im really confused


#4

I would interpret that the same way.

I can't speak for the replit site. I can't speak for codecademy. Your code is running smoothly and triggers no error messages in either setting. Maybe one of the mods on here will shed some light on this in the future.


#5

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