Having trouble


#1


https://www.codecademy.com/courses/learn-javascript/projects/rock-paper-scissors


I'm not getting any error messages but when I try to run the code I get a console.log of the entire first two fuctions written out. However if I dont call the playGame function and just run a console.log of the userChoice and computerChoice on their own they work perfectly fine. Can anybody help me out?


function userChoice() {
  var userInput = prompt('Choose your weapon');
  userInput = userInput.toLowerCase();
 if (userInput === 'rock' || 'paper' || 'scissors') {
   return userInput;
 } else {
   console.log('Invalid Choice, Pleae Try Again');
 }
}

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

function declareWinner(userChoice, computerChoice) {
  switch (userChoice) {
    case computerChoice:
      return 'Draw!';
    case 'rock':
      switch (computerChoice) {
        case 'paper':
          return 'You lose';
        case 'scissors':
          return 'You win';
                    }
    case 'paper':
      switch (computerChoice) {
        case 'rock':
          return 'You win';
        case 'scissors':
          return 'You lose';}
    case 'scissors':
      switch (computerChoice) {
        case 'rock':
          return 'You lose';
        case 'paper':
          return 'You win';
                            }
                    }
}

function playGame() {
  var getUserChoice = userChoice;
  var getComputerChoice = computerChoice;
  console.log('You threw ' + getUserChoice + 'the computer threw ' + getComputerChoice);
console.log(declareWinner(userChoice, computerChoice));
}

playGame();


#2

Hey there, I'd be glad to help.

1.

To solve your initial problem we have to add some parentheses.

Let's take var getUserChoice = userChoicee; for example in your playGame function.

In this instance you're giving getUserChoice a reference to the userChoice function itself. You're not actually executing any of the code inside of it. If you were to then do getUserChoice() - with parenthese, then you would be calling the referenced function and executing the code. This is why in console.log when you reference getUserChoice you're getting the string representation of what the function is because you didn't include (). You could call it in the console.log section but that's not what we intend for.

SO for..

var getUserChoice = userChoice;
var getComputerChoice = computerChoice;

We need to call userChoice and computerChoice by adding the set of parentheses at the end. And by calling those functions they will return the result and assign it to getUserChoice and getComputerChoice. That's better.

2.

We're not in the clear yet however.

In your last console.log statement, there's going to be an issue when passing the parameter values for declareWinner(). See, you should be including getUserChoice and getComputerChoice because those variables already contain our values for which we will be determining a winner.

Hope that makes sense, let me know how it goes.


#3

Thanks for the help, that did the trick.


#4

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