Pls help computer choice


#1

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

thats what i have and i keep getting syntax error


#2

With each option, you need to assign the option to computerChoice. So:

computerChoice = "rock"

It's as the same for the other ones, too. :smiley:


#3

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

still says illegal return statement


#4

Also return is only allowed to be used inside of functions. It somehow manages the return from the scope of the function back to the main scope so using it on the main scope is pointless and will cause an error. And this won't work:

 (computerChoice>=0.34&<=0.66)

first of all & is the binary version of the boolean operator e.g. 4&7 = 4 because

111 = 7
100 = 4
1&1 = 1| 1&0 = 0| 1&0=0 -> 100 = 4

what you should use here is && which is true if the left and the right value next to it are true and false otherwise. The other problem with it is this:

<=0.66

<= compares two values but there you have only one. Better use computerChoice <= 0.66 instead here. Also maybe think if the first part is even necessary.


#5

Adding on to what @haxor789 said, & does not mean and. In JS, and means &&. :smiley:


#6

It means AND :frowning:
But it's the AND for binaries as I tried to explain. But as you rather won't use this often if you're not looking for too deep high performance, you're right && is what you're looking for. :smile:


#7

Oh.... haha :smiley:

Just that I don't think they ever taught about that in the courses.... like, I found out about '==' in the glossary... :smiley:


#8

There once was a non-tracked course that told about the use of these binary operators which was pretty good if I find it again I'll post it.
https://www.codecademy.com/courses/introduction-to-bitwise-operators/0/1


#9

Where did you find this?! :confused:

I can see one similar like that in Python, but it seems like JavaScript in the link.... mysterious!:smiley:

But I'll try the level out, thanks!


#10

I knew the name because I did it earlier and then googled it :smile: