Keeps printing "scissors" when the choice should be paper!


#1


var computerChoice= Math.random()
{
console.log(computerChoice)
}

if(0<computerChoice<.33)
{
    computerChoice= "rock"
    console.log(computerChoice);
    }
 else if (.34<computerChoice<.66)
 {
    computerChoice= "paper"
    console.log(computerChoice);
    }
    else
    { 
         computerChoice= "scissors"
        console.log(computerChoice);
    }


#2

JavaScript does not support chained comparisons. So this code:

0 < computerChoice <= .33

should be turned into:

0 < computerChoice && computerChoice <= 0.33

But you know that Math.random() does not return the negative values so you can simply write:

computerChoice <= 0.33

In the else if you know that condition of if was false so you don't have to check if computerChoice < 0.33, you know this is true, so you can use:

computerChoice <= 0.66

What is the purpose of braces in this part of code:

var computerChoice= Math.random()
{
console.log(computerChoice)
}

?


#3

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