Getting a weird result in Rock Paper Scissors

Hi everyone,

I just completed the challenge ( https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/learn-javascript-functions/projects/rock-paper-scissors-javascript ), and I am getting the correct results, so it will show a tie, and who won, but on top of that, just about all my code is being printed as well. I have looked at the video, and I just can’t see what I am doing wrong.

Here is my code:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('You can only type rock, paper or scissors');
  }
};

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

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'Tie';
  } 
    if (userChoice === 'rock') {
      if (computerChoice === 'paper') {
      return 'Computer has won'
    } else {
      return 'User has won';
    }
    }
    
    if (userChoice === 'paper') {
      if (computerChoice === 'scissors') {
      return 'Computer has won';
    } else {
      return 'User has won';
    }
    }
    
    if (userChoice === 'scissors') {
      if (computerChoice === 'rock') {
      return 'Computer has won';
    } else {
    return 'User has one';
    }
    }
  
  if (userChoice === 'bomb') {
    return 'Congrats, you won!'
  }
  };

const playGame = () => {
  const userChoice = getUserChoice('bomb');
  const computerChoice = getComputerChoice();
  console.log('User Threw:' + getUserChoice);
  console.log('Computer Threw:' + getComputerChoice);
  console.log(determineWinner(userChoice, computerChoice));
};

playGame();

And this is what I get in the browser:

User Threw:userInput => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’ || userInput === ‘bomb’) {
return userInput;
} else {
console.log(‘You can only type rock, paper or scissors’);
}
}
Computer Threw:() => {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return ‘rock’
case 1:
return ‘paper’;
case 2:
return ‘scissors’;
}
}
Congrats, you won!

I am not sure where I have gone wrong?

Kevin

Something weird is printed -> investigate use of console.log

1 Like

Anyway, speaking of printing out functions, I actually did this for rps.
Well, sort of. I first gave them toString properties.

So rock is a function that tests whether its argument is scissors.
So to test if rock beats something, you’d do:
rock(something)
or
const p1wins = player1(player2)

const choose = xs => xs[Math.floor(Math.random() * xs.length)]
const caseOf = (x, cases) => new Map(cases.slice().reverse()).get(x)

const tie = Object()
const left = Object()
const right = Object()

const bomb = () => true
const rock = other => other === scissors
const scissors = other => other === paper
const paper = other => other === rock
bomb.toString = () => 'bomb'
rock.toString = () => 'rock'
paper.toString = () => 'paper'
scissors.toString = () => 'scissors'

const getComputerChoice = () => choose([rock, paper, scissors, bomb])
const determineWinner = (a, b) => (a(b) === b(a) ? tie : a(b) ? left : right)

;(() => {
  const userChoice = rock
  const computerChoice = getComputerChoice()

  console.log('You threw: ' + userChoice)  // <-- userChoice is a function
  console.log('The computer threw: ' + computerChoice)
  console.log(
    caseOf(determineWinner(userChoice, computerChoice), [
      [tie, 'The game is a tie'],
      [left, 'Congratulations, you won!'],
      [right, 'Sorry, the computer won.']
    ])
  )
})()

Thanks for the tip ionatan. Had a look through all the console.log and within a minute I had found the issue.

I see I was calling the functions rather than the variables in the last block.

You have no idea how many times I looked over it and didn’t realize!

Thanks for giving a hint and not actually giving a fix, much better to learn through hints than just straight answers.

Thanks,
Kevin

Mmmh. No. Then you would have printed the return value. You printed the function itself.
So, your javascript implementation (presumably node) uses the code source of the function as string representation for arrow functions. Or something like that.

>>> ''+(() => 1)
'() => 1'

Ah yea, you are right. Thanks.

If I had to call it I would have used functionName();
correct?

Yeah.
So you could for example put functions in an array

and call them at a later time

1 Like

Okay, I see. Thanks! :wink:

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