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")
}
1
  • 5
    That recursive userChoice is looking mighty suspect. Commented Feb 2, 2014 at 1:56

1 Answer 1

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());
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.