Computer Choice 2: Wrong ranges?


#1

I'm performing the exercise Build Rock, Paper, Scissors: computer choice 2.

https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/0/4?curriculum_id=506324b3a7dffd00020bf661#

I do not receive an error message, but common sense tells me something is wrong. When I run the code any value between 0 and 1 yields the outcome 'scissors', which is not in line with expectations. According to the exercise I'm fine to go on, according me I'm not.

I changed the notation of the ranges within the code several times, but for overview sake, I stuck with the one below. What is going wrong here?


//Rock destroys scissors
//Scissors cuts paper
//Paper covers rock

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

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

// Setting the ranges below. 
// Ranges don't work as they should. 0.2 still yields scissors.

if (computerChoice <= 0.33)
{
    computerChoice = "rock";
}

else if (computerChoice <= 0.66)
{
    computerChoice = "paper";
}

else (computerChoice >= 0.67)
{
    computerChoice = "scissors";
}
};

Thanks!
Peter


#2

Well using common sense and see that something is wrong without getting an error message is pretty good, because to be quite honest these are the more nasty bugs as you have few indication where to search for.

About the problem: Well it has to do with your else case. If you have a look at this:

I explained there that it is fine to omit the {} after conditionals like (if,else) if there is only one statement following. Now if you have a look at your else:

else (computerChoice >= 0.67)
{
    computerChoice = "scissors";
}

The problem is that else does not have a condition. So (computerChoice >= 0.67) will not be interpreted as a condition but rather the statement that is conditioned by the else leaving the rest to be executed no matter what. This way computerChoice gets always reassigned with "scissors". So the most simple way to fix it would be to just get rid of this pseudo condition because after all you don't need it anyway. Think about it, you're range of numbers goes from 0 to almost 1 and you've already dealt with 0 to 0.66 so that range is what is left anyway :slight_smile:

Hope this makes sense to you otherwise just respond and I'll give it another try.


#3

Thanks for the quick reply! I was having myself a Homer-moment ("DOH!") as I read the last part of your answer. I changed it, and now indeed, it works correctly.

The problem is that else does not have a condition. So (computerChoice >= 0.67) will not be interpreted as a condition but rather the statement that is conditioned by the else leaving the rest to be executed no matter what. This way computerChoice gets always reassigned with "scissors".

Also, thanks for the thread you linked to. Clarifies a lot!

Cheers,
Peter


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.