0

I'm attempting to debug this for a course on Codecademy and can't find what I'm doing wrong. If anyone could help it would be immensely helpful. Thank you for taking the time to read this message. Here's the code:

var userChoice = function (string) {
    userChoice(window.prompt("Do you choose rock, paper, or scissors?"));
}

var computerChoice = Math.random();
console.log(computerChoice)

if (computerChoice === 0 to 0.33) {
    console.log("rock")
} else if (computerChoice === 0.34 to 0.66) {
    console.log("paper")
} else if (computerChoice === 0.67 to 1) {
    console.log("scissors")
}
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
danteMingione
  • 75
  • 1
  • 2
  • 7

1 Answers1

5
if (computerChoice === 0 to 0.33) {

That isn't valid. Try this:

if (computerChoice >=0 && computerChoice <=0.33) {

Beware though that there are numbers between 0.33 and 0.34! Your second condition should be computerChoice >0.33 rather than using 0.34.

If you're really trying to select a random item from a list of items though, consider an array. See this post: Get random item from JavaScript array

var items = ['rock', 'paper', 'scissors'];
console.log(items[Math.floor(Math.random()*items.length)]);

The array items in this case contains all possible choices. We get a random number and multiply by the number of possible items (items.length). Then, we use Math.floor() to ensure a nice integer rather than a float (non-whole number), which is expected for array offsets.

Finally, see @eddflrs' note about your userChoice() function. If you want to get user input, why would you continually call your function recursively? Try this:

function getUserChoice () {
    return window.prompt('Do you choose rock, paper, or scissors?');
}
console.log(getUserChoice());
Community
  • 1
  • 1
Brad
  • 159,648
  • 54
  • 349
  • 530
  • I figured it was in that part of the code. I wasn't sure how to include a value between two numbers. So thank you for that. Although the array and the updated user input would be helpful I'm doing codecademy.com so they're extremely rigid on what I can type so they can check my work before advancing onto the next lesson. However, they are wonderful suggestions and I'll be reading that literature you linked me on arrays. I'm extremely grateful for this website and the people willing to help me. – danteMingione Feb 02 '14 at 05:07