Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. Rock, Paper, Scissors game

Hi, I began JavaScript on Codeacademy a couple of days ago and so far it's been okay. But I've run into a problem designing a rock, paper, scissors game for one of the exercises. I tried posting my question on the Codeacademy forums but no one has replied. This is my code for the game. Currently, the randomized computer choice function works. But as you can see, I've only used < and >. Therefore, if the computer choice is somehow exactly .33 or .66, the computer won't be able to make a decision because I have not told it which choice to pick. However, when I add a <= or >=, I get an ") expected" error. How can i change my code to include .33 and .66?

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

2. You need to use the logical operators for less than, more than, and for AND and OR as well. So study up on the logical operators.

3. if(computerChoice <= .33) {
computerChoice = "rock"
}else if(computerChoice <= .66) {
computerChoice = "paper"
}else {
computerChoice = "scissors"
}

4. Not really sure if that's what you're looking for but the code below works under Firefox and Webkit.

Code:
```var userChoice = prompt("Do you choose rock, paper, or scissors?");
var computerChoice = Math.random();
console.log(computerChoice);
if(0 <= computerChoice && computerChoice <= .33) {
computerChoice = "rock"
}
else if(computerChoice > .33 && computerChoice <= .66) {
computerChoice = "paper"
}
else {
computerChoice = "scissors"
}```

5. Originally Posted by azimdj5
However, when I add a <= or >=, I get an ") expected" error. How can i change my code to include .33 and .66?
Are you sure that your syntax is correct >= and <=, not =< or =>

6. Use
Code:
`var computerChoice = Math.ceil(Math.random()*3);`
so that you have a choice of 1, 2 or 3 and then you can use ==
Code:
```if (computerChoice == 1) {
computerChoice = "rock";
}
else if (computerChoice == 2) {
computerChoice = "paper";
}
else {
computerChoice = "scissors";
}```

7. Originally Posted by glenngv
Use
Code:
`var computerChoice = Math.ceil(Math.random()*3);`
so that you have a choice of 1, 2 or 3 and then you can use ==
Ummm...not entirely true. There is a VANISHINGLY SMALL but still finitely possible chance that Math.random() can be *EXACTLY* zero!

And so it is POSSIBLE that the value for computerChoice using your code will be zero, not 1 or 2 or 3.

This admittedly very small chance for a zero result is the reason that code would be better written as
Code:
`    var computerChoice = 1 + Math.floor(Math.random()*3);`

8. Of course, the best way to write it (well..."best" in the sense of most obscure to a newbie programmer <grin/>) would maybe be:
Code:
`var computerChoice = ["rock","scissors","paper"][ Math.floor( Math.random() * 3 ) ];`

9. Originally Posted by Old Pedant
Ummm...not entirely true. There is a VANISHINGLY SMALL but still finitely possible chance that Math.random() can be *EXACTLY* zero!

And so it is POSSIBLE that the value for computerChoice using your code will be zero, not 1 or 2 or 3.

This admittedly very small chance for a zero result is the reason that code would be better written as
Code:
`    var computerChoice = 1 + Math.floor(Math.random()*3);`
Good catch.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•