Rock, Paper, scissors reference error


#1

—Well, I appreciate the help so far. I have reached a new wall. my new error tells me:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:32
if (userChoice === ‘paper’) {
^

ReferenceError: userChoice is not defined
at Object.

—so, I am having trouble understanding. I wonder why “user choice is not defined” on line 32 but seems to be “user choice” on line 24 is just fine. I can’t see the difference. thanks again for any help. heres the code:

function determineWinner (userChoice, computerChoice) {
if (userChoice === computerChoice) {
return ‘game was a tie!’;
}
if (userChoice === ‘rock’) {
if (computerChoice === ‘paper’) {
return ‘computer won!’;
} else {
return ‘user won’;
}
}
}
if (userChoice === ‘paper’) {
if (computerChoice === ‘scissors’) {
return ‘computer won!’;
} else {
return ‘user won’;
}
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’) {
return ‘computer won’;
} else {
return ‘user won’;
}
}


#2
function determineWinner (userChoice,            computerChoice) { 
  if (userChoice === computerChoice) {
   return 'game was a tie!';
  }
   if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
  return 'computer won!';
  } else  {
   return 'user won';
       }
      }
} // this } closes function

see comment, you close your function a bit too early


#3

OMG! that one little thing solved the whole problem! works like a charm! thank you!


#4

Having proper indent help solve problems like these, its easier to matching brackets then.


#5

a better editor may have caught that.


#6

Do you have prove for that? Not all errors can be caught, no matter how good the editor. Nothing is stopping anyone from using jslint to help you with the errors


#7

No. But you must admit that many editors have a “matching braces” feature.


#8

the braces are matching, } is just not on the right spot, so matching braces feature will very likely not help


#9

So as far as I understand, “matching” means different things for different languages. In the case of Python, wouldn’t matching include indentation as well as pairing?


#10

pairing of what?

yep, python relies on indent, no curly brackets used. So then indent levels need to match, but even that doesn’t give absolute guarantees:

def example(x):
   r = []
   for i in x:
      r.append(i ** 2)
      return r

print example([1, 2, 3])

all the indent levels are matching (so valid code), yet not all elements are added to r.


#11

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