"Rock, Paper, Scissors"


#1


Build "Rock, Paper, Scissors"


In console I do not receive the message with who the winner is even tho' I use console log to display just that. Can anyone tell me why, please?


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);
console.log("User: " + userChoice);
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 === "paper"){
            return "scissors wins";    
        }
        else{
            return "rock wins"    
        }    
    }
    console.log( compare(userChoice, computerChoice) );
    
}

}


#2

there is a bit of a problem in this bit of your code:

// function call
console.log( compare(userChoice, computerChoice) );
} // this bracket closes function

} // a additional bracket which doesn't serve a purpose

you should close your function before you call your function, and you seem to have have } to many, see comments in the code i made


#3

You seem to have an extra } in your code!
Also call the compare function outside of the function!


#4

Isn't that } supposed to close the Else If function?


#5

Ahh ok I have copied the text wrong...but the problem is still there


#6

nope? it is not, look:

else if(choice1 === "scissors"){
        if(choice2 === "paper"){
            return "scissors wins";    
        }
        else{
            return "rock wins"    
        } // closes else
    }  // close else if
    console.log( compare(userChoice, computerChoice) );
    
}  // closes function
  // function call should be here
} // this one doesn't do anything

see comments i made in the code


#7

I have seen it I am sorry I have miscopied the code. That braket was not there in my code ...the problem was that i was calling the function inside the function


#8

the most important question: Did you get it to work and do you understand why it works?


#9

Yes now it works and now I understand how it works and that I always have to call a function OUTSIDE it


#10

Not always, but that is more advanced. That is for later, but i don't want to lie to you :stuck_out_tongue:

For now, place the function calls outside the functions


#11

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