Rock, Paper, Scissors Project

Rock, Paper, Scissors Project

Hello all, I’d appreciate a little help here. I am learning functions and have just completed the last step of ‘Rock, Paper, Scissors’.

This is what I have:

const getUserChoice = (userInput = userInput.toLowerCase()) => {
  if (userInput === 'rock' | 'paper' | 'scissors'){
    return userInput;
  }
  else {
    console.log('Not a valid choice.')
  }
}
const getComputerChoice = () => {
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
  }
}


function determineWinner(userChoice, computerChoice) {
  if (userChoice == computerChoice){
    return console.log('Game was a tie!');
  }
  if (userChoice == 'rock'){
    if (computerChoice == 'scissors'){
      return 'User won!'
    }
    else {
      return 'Computer won!'
    }
  }
  if (userChoice == 'scissors'){
    if (computerChoice == 'paper') {
      return 'User won!'
    }
    else {
      return 'Computer won!'
    }
  }
  if (userChoice == 'paper') {
    if (computerChoice == 'rock') {
      return 'User won!'
    }
  }
  else {
    return 'Computer won!'
   
  }
}

function playGame () {
  const userChoice = getUserChoice('rock' | 'paper' | 'scissors');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice)
  console.log(determineWinner('scissors', 'paper'))
  }

playGame()

Regardless of what I pass in the parameters of the last line, I get:

Not a valid choice.
You threw: undefined
The computer threw: paper
User won!

Can you guys tell me what I’m doing wrong? Much appreciated.

1 Like

How is the selection made for the user? I don’t see a function or callback that will allow the user to either pick rock paper or scissors, or a random picker for the game.

3 Likes

Thanks for the reply - with that in mind I’ll see if I can get it working.

Your console output already greatly narrows down where things go wrong, so maybe if you started playing around with those actions by themselves without the rest of the code to distract? What’s the earliest point in the program where things stop behaving like you want, and which exact operation was it that misbehaved, what did you mean for that to do and how can you make that happen?

4 Likes

Thanks for your reply, I love this community already. I will go over it and hopefully come back with some good news.

1 Like

Also please leave out the semi-colons instead of kinda just sprinkling them around.
Put them where they belong, or don’t.
My guess is you can’t figure out where they go anyway, so the simpler option is to leave them out. As for how you’d figure, you’d consider whether or not a statement of that particular kind ends in semi-colon. for example, an assigment statent does end in semi-colon and so you’d therefore …

const blah = () => {
  ...
  ...
}; // <---------
2 Likes

Admittedly, yes… I’m having trouble with whether or not to end with a semi-colon! Thanks for the advice.

1 Like

Not saying it’s reasonable know it.
Just that you may as well… not.
There are two correct ways to do it, one of them is easy, the other isn’t.

4 Likes

I’m back!

This is the revised code:

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  }
  else{
    console.log('Not a valid answer.')
  }
};

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 'The game is 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!';
   }
  }
}

const playGame = () =>{
  const userChoice = getUserChoice('rock');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}

playGame();

I really want to be able to use the semi-colons right though, hopefully I did…

3 Likes

I think you did pretty well, but like @ionatan it doesn’t really make a huge difference. It doesn’t really matter if you put them in or not, as long as if you do, you put them in correctly

3 Likes

do I get to nitpick on that? I suppose it’s the exercise’s design really, but this

const determineWinner = (userChoice, computerChoice) =>{
	if(userChoice === computerChoice){
    return 'The game is 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!';
   }
  }
}

Is such a huge mess, makes me nervous about whether it’s correct and trying to verify that is … It’s an easy problem but it’s already hard.
The logic it describes fits in a smaller space.

rock beats scissors
scissors beats paper
paper beats rock

…it’s a list.

[['rock', 'scissors'],
 ['scissors', 'paper'],
 ['paper', 'rock']]

This list can be found of sorts in all those if-statements, but the rest of the logic is all the same thing repeated over and over. That logic can be written separately, once, and then applied to the list.

It could be made even smaller:

['rock', 'scissors', 'paper']
A value here beats the one on its right (with wrap-around after paper)

4 Likes

Arrays will be explained later on in the Javascript course let alone prototype functions.

But with knowledge provided by the course it could be written shorter. This is my version I wrote at that time:

function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'It\'s a tie!';
  } else if (userChoice === 'rock' && computerChoice === 'paper') {
    return 'You Lose! The computer won!';
  } else if (userChoice === 'paper' && computerChoice === 'scissors') {
    return 'You Lose! The computer won!';
  } else if (userChoice === 'scissors' && computerChoice === 'rock') {
    return 'You Lose! The computer won!';   
  } else {
    return 'Winner Winner Chicken Dinner! You won!'
  };
};
4 Likes

Interesting, that says the reverse of how the rules are usually told
rock is beaten by paper
paper is beaten by scissors
scissors is beaten by rock
(I tried to factor out the comparison and it all turned out backwards :frowning: )

function beats (a, b) {
  return (
    (a === 'paper' && b === 'rock') ||
    (a === 'rock' && b === 'scissors') ||
    (a === 'scissors' && b === 'paper')
  )
}

function determineWinner (userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'It\'s a tie!'
  } else if (beats(computerChoice, userChoice)) {
    return 'You Lose! The computer won!'
  } else {
    return 'Winner Winner Chicken Dinner! You won!'
  }
}

And if allowing use of arrays:

function beats_ (a, b) {
  const conditions = [
    ['paper', 'rock'],
    ['rock', 'scissors'],
    ['scissors', 'paper']
  ]
  return conditions.includes([a, b])
}
4 Likes

Well with the inclusion of the secret weapon ‘bomb’ I found there were 4 conditions in which the player would win oppose to 3 conditions of loss so I used the else statement for a win ;).

3 Likes

I like how this topic went from a simple problem with the code to a discussion on the most efficient way to make this game :smile:

3 Likes

No no. We’re trying to convert it to lisp. Well, I guess that’s the same thing as most efficient.

const beats = (a, b) =>
  [['paper', 'rock'],
   ['rock', 'scissors'],
   ['scissors', 'paper']
  ].includes([a, b])

const determineWinner = (userChoice, computerChoice) =>
  cond(
    [userChoice === computerChoice, 'It\'s a tie!'],
    [beats(computerChoice, userChoice), 'You Lose! The computer won!'],
    [true, 'Winner Winner Chicken Dinner! You won!'])

const cond = (...args) =>
  args.filter(([condition, _]) => condition)[0][1]

console.log(determineWinner('rock', 'paper'))

edit: oh my beats function doesn’t even work. I blame javascript.

> [[]].includes([])
false
5 Likes

Hey I also need some help on this ‘Rock Paper Scissors’ Project. I have gotten this far and every time I run the code a syntax error keeps coming up. I have re-written the code many times from scratch and watched the help video but I can’t figure out my mistake. Please let me know if you see what the problem is! Thanks!

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  }
  else {
   console.log('Error!')
  }
};

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


This is the error:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:11
const getComputerChoice => {
      ^^^^^^^^^^^^^^^^^
SyntaxError: Missing initializer in const declaration

Looks like you’re still working on this, but the reason for your error is your getComputerChoice declaration. This is a function, but you haven’t used proper arrow function syntax. Compare the two functions that you have:

const getUserChoice = (userInput) => { //parenthesis around userInput are optional
const getComputerChoice => {

Notice a difference? Remember that when using arrow syntax if there are no parameters we must include an empty set of parenthesis ( ). Side note: the parenthesis are optional with only one parameter.

Click on the spoiler if you’re still stuck:
SPOILER:

const getComputerChoice = () => {
  //function code block
}

Happy coding!

1 Like

I have been looking at this same line for an hour and still wasn’t able to figure it out. thank you so much for your help!

2 Likes

You’re welcome! :slightly_smiling_face: