Undefined error in computerChoice


#1

Can anyone look at my code and offer some thoughts??
I am getting an undefined to the console and I think it comes from the computerChoice function. I think it has to do with what I am doing with the random number in lines 15-18 but I’m not sure how to fix it.

Here is gist with entire link


#2

Boolean operators should normally take boolean values as input, you’re giving || strings as input


#3

Thank you… that is the userChoice function and that one works perfectly. It is also shown that way in the hint section of day4. It is the computerChoice function that is not working properly.


#4

Then you have at least two bugs. No that does not work perfectly, because you have a condition in your if-statement that always evaluates to something truthy, it does not test anything. When you say it works perfectly you mean that one of the two branches it can take works perfectly, but that’s the thing, an if-statement has two branches it can take, so they both need to be correct.

your getComputerChoice function looks fine to me, though it has a couple of breaks that aren’t needed because they can never be reached because you’ve already returned first, and there’s also an unreachable default branch which will also never be reached and can therefore also be removed

And, generally, I suggest that you indent by either 2 or 4 spaces (pick one and use everywhere) and remove the “end of” comments. Also, avoid tabs, or at least, if you use tabs then only use tabs, not mixed with spaces.

for example, instead of:

  if (userChoice === 'scissors') {
  	if (computerChoice === 'rock') {
    	return 'The computer won!';
  } else {
    	return 'You won!';
  }        
} //end userchoice is scissors

I’d much rather see:

if (userChoice === 'scissors') {
  if (computerChoice === 'rock') {
    return 'The computer won!';
  } else {
    return 'You won!';
  }
}

There’s no need for the comment when things line up.


The undefined that’s being printed is something you can chase down by adding console.log’s to your code in attempt to find it. Since it’s being printed, there’s probably a console.log involved so that tells you which value is undefined, and then you can start looking at where you got it from


#5

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