4/9 I am stucked! Please help me. :)


#1

There is problem with syntax with my code. Please HELP.

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

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


#2

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

Same here!! What went wrong?


#3

@tom_batoh

I got it!

First of all fix this:

if else(computerChoice >= 0.34 && computerChoice <= 0.66)

its else if

Look my code:

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


#4

mine worked out perfectly, heres the code.

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

var computerChoice = Math.random()
console.log(computerChoice)
if (computerChoice <=0.33) {
computerChoice = "rock";
} else if (computerChoice >= 0.34 && computerChoice <= 0.66){
computerChoice = "paper";
}
else {
computerChoice = "scissors"
}


#5

Really thank you Guys. It helped a lot. :wink:


#6

hey cant we put the else if statement like that ?

else if (0.34<=computerChoice<=0.66){
computerChoice="paper";
}


#7

Unfortunately no. The problem is that will be read as this:

0.34<=computerChoice<=0.66
(0.34<=computerChoice)<=0.66
true/false <= 0.66
false/true

The problem is that true and false have a numerical value of 1 (true) and 0 (false) so if you check the cases it does the opposite of what it is supposed to do and is not throwing an error as it is correct concerning the syntax. So this creates some kind of errors that can really frustrate you as no interpreter hints are given that this creates an unwanted output, but it does...

@tom_batoh as @scriptpro64859 already said your problem is the else if instead of if else for the second case. And @scriptpro64859 your problem was the lack of a computerChoice here in case you hadn't found out by yourself:

 else if (computerChoice >= 0.34 && /*computerChoice*/ <= 0.66) {

#8

thank you bro for your answer it is really helpful