Computer Choice Part 2


#1


I keep getting "Unexpected token "<=...Somebody please help me I can't figure out what's wrong! :frowning:


var userChoice=prompt("Do you choose rock, paper, or scissors?");

var computerChoice=Math.random();

if (computerChoice<.34){
    
    computerChoice="rock";
}
else if (computerChoice >=.34 && <=.66){

    computerChoice="paper";
}
else{
    
    computerChoice="scissors";
}


#2

Since it will be rock from 0 to 0.34, paper from 0.35 to 0.67, and scissors from etc., I suggest you use the following code:

if(computerChoice < 0.34){
computerChoice = "rock";
}
else if(computerChoice <= 0.67){ //If it's smaller or equal to .67, and you do
//not need to type .66, since it ends at .67 (before it)
computerChoice = "paper";
}
else{ //else only, since it already knows until what number
computerChoice = "scissors";
};

I hope this helps!
If it does, click the "Solved" button below this reply.


#3

no it doesn't work........................


#4

This is what I found:

var userChoice = prompt("Do you choose rock, paper or scissors?");
        computerChoice = Math.random();


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

#5

That is great but you have to explain what you found and how it possibly solves the problem


#6

Yep I'm quite new to that :). I've use similar logic as @lance347, I guess the rest was just javascript syntax.

to explain:
If computer choice number is under 0.33 then it's a rock
If computer choice number is above 0.67 then it's scissors
if it's anything else (implying anything between 0.33 and 0.67) then it's paper

Hope that helps


#7

It's work...:slight_smile: Here my code

var userChoice=prompt("Do you choose rock, paper or scissors?");
var computerChoice=Math.random()
if(computerChoice <= 0.33 ){
computerChoice="rock"
}else if (computerChoice > 0.67)
{
computerChoice="scissors"
}else{
computerChoice="paper"
};
console.log(computerChoice);


#8

Hello! I copied your code to codeacademy task to see what is wrong. You have "SyntaxError: Unexpected token <=" .
Find the line "else if (computerChoice >=.34 && <=.66) ". You need to add the name of variable after &&, because the computer doesn't see what is smaller or equal to 0.66. So, try to write your code like this "else if (computerChoice >=.34 && computerChoice <=.66)". I hope that I helped you and you understand where is your mistake :slight_smile: