4

// beginner learning Js in a program I was asked to start work on a Rock, Paper, Scissors game this is the code I made, but the computer always choses scissors!? any idea why? any help would be awesome!

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

  2. If computerChoice is between 0.34 and 0.66, make computerChoice equal to "paper".

  3. If computerChoice is between 0.67 and 1, make computerChoice equal to "scissors".

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

var computerChoice = Math.random();

if (computerChoice === (0 , 0.33)) {
    console.log("rock");
} else if (computerChoice === (0.34 , 0.66)) {
    console.log("paper");
} else {
    console.log("scissors");
}
user2864740
  • 54,112
  • 10
  • 112
  • 187
user3599708
  • 43
  • 1
  • 4
  • 8
    Languages tend to work best when you don't make up your own syntax, and expect it to work. Where did you come up with `computerChoice === (0 , 0.33)`? – Jonathon Reinhart May 03 '14 at 18:39
  • Duplicate: http://stackoverflow.com/questions/6454198/javascript-check-a-range-of-numbers-in-if-statement (I used up all of my close votes). – Jonathon Reinhart May 03 '14 at 18:40
  • Would this help simplify what you're checking against? http://stackoverflow.com/questions/1527803/generating-random-numbers-in-javascript-in-a-specific-range Set the range to 1 - 3 and check against that.. – gratz May 03 '14 at 18:48
  • Aren't you supposed to ask on codecademy? – bjb568 May 03 '14 at 18:53

2 Answers2

8

First off, you can't just make up syntax - it Just Doesn't Work. Programming is about the application of existing rules, where most rules are not your own.

In JavaScript, due to the comma-operator, x === (a, b) is semantically equivalent to x === b (when the a expression has no side-effects), so that clearly won't work. In the original code this ensured that none of the first two conditions (e.g choice === 0.33) ever matched - so the default/else of "scissors" was always chosen.

While the general way to check an inclusive range is x >= a && x <= b, we can do one better than this by relying on the ordering of if conditional evaluation and "inchworming" through an expanding range.

if (choice <= 1/3) {
  console.log("rock");
} else if (choice <= 2/3) { /* choice > 1/3, per above */
  console.log("paper");
} else {                    /* choice > 2/3, per above */
  console.log("scissors");
}

Alternatively, we could also get a discreet random integer back,

/* get a random number 1, 2, or 3 */
var choice = Math.floor(Math.random() * 3 + 1);

and then direct equality without a range check can be used

if (choice === 1) {
  console.log("rock");
} else if (choice === 2) {
  console.log("paper");
} else { /* choice === 3 */
  console.log("scissors");
}
Community
  • 1
  • 1
user2864740
  • 54,112
  • 10
  • 112
  • 187
1

You're not using valid JavaScript syntax for numeric comparison. There is no structure in JavaScript that allows you to do this:

if(computerChoice===(0, 0.33)){
    //...
}

Try this instead:

if(computerChoice > 0 && computerChoice <= 0.33){
    //...
}
Ethan Brown
  • 24,393
  • 2
  • 71
  • 89
  • Devil's advocate: JavaScript *does* allow the first syntax .. it just doesn't, uhh, have the "between" behavior. – user2864740 May 03 '14 at 18:44
  • A bit off-topic, but CoffeeScript would allow `if 0 < computerChoice < 0.33`. – Seiyria May 03 '14 at 18:55
  • @Seiyria That's cool. It looks like it uses similar expansion rules to Python (which also allows this sort of conditional chaining): `1 < x < 2` (CS) is percolated to `1 < x && x < 2` (JS). – user2864740 May 03 '14 at 19:01
  • 1
    Good point, @user2865740. Allowed, in this case, however, does not equal useful. `(0, 0.33)` will simply evaluate to `0.33`, making this statement completely pointless. But your point is taken! – Ethan Brown May 03 '14 at 19:35