Why does this work?


#1


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


I have completed the "Rock, Paper, Scissors" and it works like a charm!
But I'm trying to figure out, why is it printing out the compare-function, since I'm only telling it to return the result, not console.log it.

Thank you!


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

Hi it's log it because it's inside the compare function and you call it

compare(userChoice, computerChoice);

An example

var hello = function(planet){
   return "Hello " + planet;
}

#call the functon here
hello("Earth");

#Ouput
Hello Earth

#3

can someone please tell why the output is only showing

computer: scissor

what about the userchoice why is it only showing computer choice


#4

Hi you didn't post your code But I'll guese that you have this line on your code

console.log("Computer: " + computerChoice);

Right?
so you can also add the userChoice like that

console.log("userChoice: " + userChoice);

SO it will print

computer: paper
userChoice: scissor

#5

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