Why do always show me a "rock"?


#1

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice);
if( computerChoice < 0,34) {
computerChoice = "rock";
}
else if (0.34 < computerChoice && computerChoice < 0.67) {
computerChoice = "paper";
}
else {
computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);


#2

this line:

if( computerChoice < 0,34) {

spot anything wrong? I do, if you want to use a decimal number you should use a dot (.) not a comma (,)


#3

The longer explanation what this does:
The comma operator (not introduced in the course) chains 2 statements to 1 statement and has the value of the last statement e.g. in situations where only 1 statement is allowed the comma-operator lets you use 2 statements.

if(B=A, B<C){
 code
}

here B<C determines the condition for the if and B=A is just a statement that for whatever reasons is put there as well. So what happens in your case is that:

computerChoice < 0

is the first part that is just evaluated and not used for the condition and 34 is your actual condition. If you now have a look at http://www.sitepoint.com/javascript-truthy-falsy/ you can read that all non 0 numbers are treated as true and that is the reason why you always end up on rock. Hope this excursion was helpful and not confusing.