Where have i gone wrong?


#1


userChoice = prompt("Do you choose rock, paper or scissors?");
/* using a variable that allows the computer to choose randomly*/
var computerChoice = Math.random();
console.log(computerChoice);
/* using if statement to test computer choice */

if(computerChoice == 0.33 ){
    console.log ("rock");
    } else if(computerChoice == 0.34){
        computerChoice = ("paper");
        } else(computerChoice == 0.67){
            console.log("scissors");
            };

her is the code error message: SyntaxError: missing  before statement


#2

A number of issues in the code, starting with the comparisons. They should not contain ==, but <= or just <.

if (computerChoice <= 0.33) {
    computerChoice = "rock";
}
else if ( ... ) {
    ...
}
else {
    computerChoice = "scissors";
}

The above includes enough information for you to repair your code.


#4

userChoice = prompt("Do you choose rock, paper or scissors?");
/* using a variable that allows the computer to choose randomly*/
var computerChoice = Math.random();
console.log(computerChoice);
/* using if statement to test computer choice */

if(computerChoice <= 0.33 ){
computerChoice = "rock";
} else if(computerChoice <= 0.34){
computerChoice = "paper";
} else(computerChoice <= 0.67){
computerChoice = "scissors";
}

same error message


#6

else does not take a conditional expression. It is a default action.

if (computerChoice <= 0.33) {
    computerChoice = "rock";
}
else if (computerChoice < 0.67) {
    computerChoice = "paper";
}
else {
    computerChoice = "scissors";
}

#7

its pathetic. you instructions worked. but why the range for computerChoice in the instruction given if they knew that this action has a default action. waste of time in this conditional excpressions. i have here on the instruction===>
If computerChoice is between 0 and 0.33, make computerChoice equal to "rock".If computerChoice is between 0.34 and 0.66, make computerChoice equal to "paper".If computerChoice is between 0.67 and 1, make computerChoice equal to "scissors".

what i am saying is that, 0 and 0.33 should be treated as an expression of logic to test for the range of computerChoice. or maybe, i interpreted this wrongly?


#8

The instruction is a little convoluted and any learners that attempt to follow them literally, run into trouble. What this reinforces is the necessity to step back and think about the problem before jumping headlong into the deep end. At this point, most learners are not well enough prepared to take on the tricky logic of working with between, which in this case is rather misleading. Nothing much we can do about it, now.

Consider, there are three outcomes for computerChoice, 'rock', 'paper', or 'scissors'.

Now consider the return value of Math.random(). It will always be less than 1, but may equal 0. That gives us a range (interval),

{ 0 <= computerChoice < 1 }

We cannot write it like this in JavaScript, though.

if (0 <= computerChoice && computerChoice < 1)

But this is only one interval. There are three outcomes so there are three intervals.

if (0 <= computerChoice && computerChoice < 1/3)
if (1/3 <= computerChoice && computerChoice < 2/3)
if (2/3 <= computerChoice && computerChoice < 1)

This is way more code than is needed, though. Consider what happens when we remove the first operand and the operator from each of the above expressions. Given they are in order from lowest to highest, we can write,

if (computerChoice < 1/3)
else if (computerChoice < 2/3)
else if (computerChoice < 1)

Now the kicker, that last expression is not needed. It won't be read unless the first two fail, meaning the value must be in the range 2/3 <= computerChoice so it must be in this range. Hence, the default, else.

if (computerChoice < 1/3) {

}
else if (computerChoice < 2/3) {

}
else {

}

#9

thanks for the explanation


#10

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