Javascript Syntax error


#1

Trying to figure what i did wrong, also the instructions say if number is between 0 and 0.33 I'm not sure if the comparison operator i used is the right one:

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

#2

The SemiColon Always tricks you

You didnt put a semi Colon after

var computerChoice = Math.random()

That caught my eye way too quick than usual :laughing:


#3

This does not work. What you need to do here is to check if computerChoice is less than 0.33 (it's greater than 0 anyway because of the range of Math.random so maybe ignore this for now and just focus one the important boundary.)


#4

hmm thanks for the input, but that didnt fix the problem


#5

here are the instructions:

If computerChoice is between 0 and 0.33, make computerChoice equal to "rock".

I am not sure how this is suppose to look in code. Meaning what comparison operators mean "between"?


#6

Follow haxor789's Lead if mine dosent work


#7

yeah im confused as to how i check computerChoice when the value changes everytime


#8

Here is the code:

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

var computerChoice = Math.random();

console.log(computerChoice);
if (computerChoice<=0.33) {
       alert("Rock");
} else if ((computerChoice>=0.34) & (computerChoice<=0.66)) {
       alert("Paper");
} else {
       alert("Scissor");
}

but the only diffrence you had to make is change print the user and computer input. Reply to me if it dosent work. :smile:


#9

that worked thanks. Just to clarify, you put (( on ELSE IF because there were two conditions, right?


#10

By conditions if you mean like if statements yes, but if you mean like by coditons i had both if computerchoice >=0.45 and <==0.66 then no.


#11

What @haxor789 means is that in this case, Math.random only picks a random number between 0 and 1. That means that you don't have to say that if the number is between 0.33 and 0, for it would already show how it is. If this is a hard explanation, I will show you with a picture:

Let's say that this is how Math.random is used, like pulls a random number out of it.

Now, you only need one condition to show between 0 and 0.33.

See that there's only one red line? But it shows the area between 0 and 0.33.

Now to separate that with 0.67, you also only need one line, because it is with 0.33 already.

See that there's only two red lines that are needed to separate 0 to 1 into 3 parts? That's how you use the if, else if, and else loop. :smiley:

EDIT:

@amanuel2 Has already shown the code, and he had put two conditions for the else if loop, which is also a fine way of doing it. But you can read this. :smiley:

But another good way is:

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


#12

Wow you did all this lol?


#13

ok maybe this will help me explain, I tried just experimenting for my own knowledge by adding:

else ((computerChoice >=0.67) & (computerChoice <=1)) {
alert("Scissor");
};

it didnt work.


#14

wow haha thanks for this detailed explanation


#15

Uhh... yeah. I was making the drawings and when I came back, it seemed that you have already given the code. I was like "Whoops. Uhhh..."


#16

Hey @objectmaster13146,

That's an else loop. :smiley:

Else loops don't include conditions, remember? :smiley:


#17

ahh thanks that helps a lot. And no i dont remember lol


#18

You will remember Next time!


#19

About this:

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

On @objectjumper63431 masters question about the ((:
Yes the (( result from the two conditions but it's not like two conditions require two (. If you have a close look you see that it is still

else if(...)

but @amanuel2 put a pair of () around the two conditions:

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

If you have a look at:
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
You could see that >= and <= already have a higher priority than & so the () around the statements are not really needed, but I had to look it up myself and you're save to have the right order of execution when you use them.

Last but not least @amanuel2 the & is the the binary AND-Operator better use the boolean operator && instead.

The way & works is something like this:

1 | 1 | 1 = 7
1 | 0 | 0 = 4
               &
1 | 0 | 0 = 4

So in this example 7&4 -> 4 because the boolean AND- Operator just keeps those digits as one where both were 1 in the original numbers.
As booleans have a numerical value of 0 and 1 as well this also works here but it's really made for other purposes and it's better to use &&.


#20

That still confuses me lol. The 2-bit thingy :smiley:

But I GET IT NOW! (kinda)