Rock, paper, scissors. I don't even know where to look


#1

// Not sure if i made a rookie mistake or something else, but i have had a lot of trouble trying to find out what the problem is. It says SyntaxError: Unexpected token )

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock'|| userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  } 
  else {
    console.log('Input is invalid. try again.');
  }
}

function getComputerChoice() {
 switch (Math.floor(Math.random() * 3)) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
	}
}

function determineWinner(userChoice,computerChoice) {
  if(userChoice === computerChoice) {
    return 'The game is a tie!';
  }
  if(userChoice === 'rock') {
    if(computerChoice === 'paper') {
      return 'The computer wins!';
  } 
    else {
      return 'You win!';
    }
  }
  if(userChoice === 'paper') {
    if(computerChoice === 'scissors') {
      return 'The computer wins!';
  }
    else {
      return 'You win!';
    }
  }
  if(userChoice === 'scissors') {
    if(computerChoice === 'rock') {
      return 'The computer wins!';
  }
    else {
      return 'You win!';
    }
  }

#2

One minor rookie mistake (that we all make) is the missing brace at the end of the last function. Still digging for unexpected token, though.

Fix that minor bug and then call the function…

 > determineWinner(getUserChoice('rock'), getComputerChoice())
<- "The computer wins!"

#3

Thank you a whole bunch, but… Now userChoice is undefined, which at first seemed logical since i hadden’t put it in yet. but now that i did, it is still undefined.

const playGame = () => {
  const userChoice = 
getUserChoice('scissors');
  const computerChoice =
getComputerChoice();
  console.log(`You threw ${userChoice}`);
  console.log(`The computer threw:
${computerChoice}`);
}

#4

In the above, the only place the variables exist is inside the function. If you are calling determineWinner, it should be from that scope.


#5

I understand what I did wrong, but as a rookie, I don’t exactly know how to fix this. Could you explain in further detail?


#6

'Til now I’ve been responding on intuition. Beyond this I need context, meaning a link to the exercise so we can play things out. Please reply with same. Thank you.


#7

here is the link to the project:
https://www.codecademy.com/courses/learn-javascript-functions/projects/rock-paper-scissors?course_redirect=introduction-to-javascript


#8

There are two things missing above…

  1. call to determine winner
  2. call to play game

The latter would be more the case.


#10

Your error message looks like it’s missing parts, here’s what I get locally:

/tmp/derp/aeotnuh/aoeu.js:52
});
 ^

SyntaxError: Unexpected token )
    at new Script (vm.js:74:7)
    at createScript (vm.js:246:10)
    at Object.runInThisContext (vm.js:298:10)
    at Module._compile (internal/modules/cjs/loader.js:670:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
    at startup (internal/bootstrap/node.js:240:19)

The “at …” lines aren’t saying anything relevant in this case. But this says it’s line 52 which isn’t even in the code. NodeJS puts your code in a function and this line 52 is where it ends.
What makes the parenthesis unexpected is that something else is still “open”, something like this:

([[

Two ]'s need to be added before ), making ) unexpected at this point

I’d (temporarily) delete roughly half your code and run it again to determine which half the problem is in, repeat until narrowed down sufficiently to be spotted. You’d generally avoid this problem by always inserting and removing various brackets in pairs
(Except I spot the missing bracket immediately because of its effect on indentation)


#11

As for undefined

JS doesn’t clearly distinguish between something not being defined, and the value undefined

For example, the result of this function is undefined, so the result is as a matter of fact defined…

() => {}

So if something’s undefined start looking at where it was supposed to come from.
Again, like with trying to find a missing bracket, starting somewhere in the middle of events to see if the desired value is there, is a good idea because that will quickly narrow down where it got lost


#12

Thank you both for you help, but when I decided to start over, checking the script as i went. Now it works


#13

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