Rock, Paper, Scissors Project

Hello, @laasiki, and welcome to the forums.

Could you explain your code? Consider what each line does versus what you are wanting it to do. Are they the same? You may want to investigate what return does.

Hey @laasiki, welcome to the Community Forums! :smiley:

Why exactly are you using the return keyword on your second line of code? I believe the problem might be there.

1 Like

Hello,
This is my first post to the forums. If I don’t know if it’s bad form to post all the code up on a project since I am basically stuck at the end of the project? Accordingly, I haven’t posted all the code.

I am stuck on step 13 of this project. Thus far all prior console.log verfications have checked consistent with the hints and the accompanying video… Alas, the depicted Play Game Error occurs when calling the playGame function.

ReferenceError: playGame is not defined
at Object. is confusing me.

I imagine this line:

console.log(determineWinner(userChoice, computerChoice));

with the computerChoice in blue versus red of userChoice is somehow a factor , but I cannot figure out how or why this is the case.

Thank you kindly for your time in advance.

const determineWinner = (userChoice, computerChoice) => {
    if (userChoice === computerChoice){
     return 'Game was a tie';
    }
    if (userChoice === 'rock') {
      if (computerChoice === 'paper') {
        return 'Computer has won';
      } else { 
        return 'You Won!';
      }
    }
  
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors')
    {
      return 'Computer Wins!';
    } else {
      return 'You won!';
    }
   } 
 if (userChoice === 'scissors'){
   if (computerChoice === 'rock'){
     return 'Computer Wins!';
   } else {
     return 'You Won!';
   }
   }
   const playGame = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw:' + computerChoice);
   }
   console.log(determineWinner(userChoice, computerChoice));
  };

playGame()

playGame();

1 Like

Hi there @nsholk! Welcome to the forums :slight_smile:

I think your only missing some of these: }. But I’m not really sure, I would like to try your code to get a better understanding of it and be able to give you a better answer…could you possibly post all of your code? :upside_down_face:

Thanks you @yizuhi for the quick response. It’s nice to be a part of this dynamic community. This is a whole new experience; learning how to code that is.
Here you go with the full code. I hope this helps, and I hope it’s not a spoiler for others.

Cheers,
Nathan

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';
   case 1:
   return 'paper';
   case 2:
   return 'scissors'; 
 }
  }

  const determineWinner = (userChoice, computerChoice) => {
    if (userChoice === computerChoice){
     return 'Game was a tie';
    }
    if (userChoice === 'rock') {
      if (computerChoice === 'paper') {
        return 'Computer has won';
      } else { 
        return 'You Won!';
      }
    }
  
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors')
    {
      return 'Computer Wins!';
    } else {
      return 'You won!';
    }
   } 
 if (userChoice === 'scissors'){
   if (computerChoice === 'rock'){
     return 'Computer Wins!';
   } else {
     return 'You Won!';
   }
   }
   const playGame = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw:' + computerChoice);
   }
  console.log(determineWinner(userChoice, computerChoice));
  };

playGame();
1 Like

No problem, @nsholk :wink:
(Also sorry for the very late response now lol).

I found you defined the playGame() function inside the determineWinner() function. So, since playGame() is defined locally inside determineWinner(), when you try to call the playGame() function outside of determineWinner(), it throws a ReferenceError.

My advice would be to simply define playGame() outside of determineWiner(). Also, you might want to put the console.log(determineWinner(userChoice, computerChoice)); line inside the playGame() function.

Really hope this helps :upside_down_face:

My apologies for the slow action @yizuhi,

Unfortunately, I am not quite grasping the execution of the concept of defining playGame() function outside of the determineWinner()function. As a result I still receive the same error as before.

In essence does every “{” have to have an associated “}” in a function? I am wondering
if I am “out of balance” with forward and back facing curly parentheses in this exercise and thus I am still inside the function but I don’t see it?

Also lastly, when I leave out the playGame( ); at the end of the exercise, the error disappears but of course I don’t end up playing the game :face_with_hand_over_mouth:

I know I am missing something here. I’m sorry I feel so close thanks to your help, but I’m still not totally connecting the concept in my head.

regards from the east coast USA =bedtime.

const determineWinner = (userChoice, computerChoice) => {
    if (userChoice === computerChoice){
     return 'Game was a tie';
    }
    if (userChoice === 'rock') {
      if (computerChoice === 'paper') {
        return 'Computer has won';
      } else { 
        return 'You Won!';
      }
    }
    
  
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors')
    {
      return 'Computer Wins!';
    } else {
      return 'You won!';
    }
   } 
 if (userChoice === 'scissors'){
   if (computerChoice === 'rock'){
     return 'Computer Wins!';
   } else {
     return 'You Won!';
   }
   };

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

hekki there **[quote=“laasiki, post:1, topic:495054, full:true”]
Hi everyone,

I’m having trouble just at the start of this project.
I’m trying to test out my initial function and I seem to be unable to get the ‘Error’ message when put it an Invalid Input…

After testing it, I checked my code against the get help video and it seems to be the same… not sure why then my if/else statement isn’t working…

Here’s the code:

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

console.log(getUserChoice(‘Paper’));
console.log(getUserChoice(‘Sun’));
[/quote]

1 Like

Hello @nsholk. The reason for your getUserChoice function not returning an error message when an invalid input is entered is because of this line:

This line returns the lower case version of userInput. When a return statement is reached, the function stops executing. That means all of this code:

Does not run.

Yes, that is correct; every { must have an associated }, as that indicates to the compiler that the block/object has ended. Same goes for everything (blocks and objects) in JS:

if (condition) {
//must have matching {}
}

function something() {
//must have matching {}
}

const function1 = () => {
//must have matching {}
}

As well, you shouldn’t have a semicolon ; after a }:

function something() {
//code
}
//correct syntax

function somethingelse() {
//code
};
//incorrect syntax

In JS, any object (functions included) has a scope. When something is defined outside of any {}, i.e. a global variable, it can be accessed anywhere:

function abc() {
console.log("cde");
}
//can be accessed anywhere in the program

However, when you use the keyword const, the objects you create then have block scope. This means that you can only access the object within the block you created it, otherwise it will be undefined:

function someBlock() {
const x = () => {
console.log("foobar");
}
x();//This will print "foobar", as you are accessing it in the correct block
//, as in, the block you defined it in.
}
x()//This throws an error, as it is outside of the block you created it in

This article explains this concept nicely.
This article also explains the use of const, var, let, and how they fit into block/function scope nicely.

I hope this helps!

1 Like

Thank you @codeneutrino for the explanations and links to further studies. I am happy to say I completed the project by correcting an imbalance of { } In the code. As both you and @yizuhi pointed out, this was an area I needed to organize. However, I couldn’t have done that without this help.

Thanks for the excellent guidance on the global vs non-global explanation too.

Lastly, can I inquire why in the enclosed screenshot is there a color contrast between the hint to question 1 and my code?

Regards,

2 Likes

Hello @nsholk, I’m glad we could help! To the matter of the syntax highlighting, well; if the code works normally, then don’t worry about it; it’s probably just a mess up on CC’s behalf, either with that exercise, or time.

brilliant, thank you @codeneutrino. I appreciate that insight. Off to the sleep calculator project I go now. Thanks for your assistance on this thread.

1 Like

const getUserchoice = userInput =>
{
userInput = userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘scissors’ || userInput === ‘paper’) {
return userInput;
} else {
console.log(‘Error, please type valid data’);
}
}
const getComputerChoice = () => {
console.log(Math.random())
};

Please what am I doing wrong, I’ve been doing this for hours now and I cannot find the mistake

If I were your student, how would you explain what this function is doing?

Also, please review How do I format code in my posts?

I guess it is telling that there could be only integer numbers but I still do not get it and the console has been showing errors throughout the whole exercise so there has to a syntax problem I am not seeing.

Well, I’m about to be away from a computer for a while, so I won’t be of much help beyond this until much later.

In order for the randomly generated value to be useful to your program, you’ll want to return the result. console.log() only prints the argument you pass it to the console. That’s it. The only reason for that is so that we humans can see it. The value is of no use to the rest of the program unless it is returned.

If you look at instruction #5 it shows (along with the hint) exactly how to use Math.random() to generate an integer. You want one of three possible values. One to represent rock, one for scissors and one for paper. As you’ve likely seen in your console, Math.random() by itself generates a value like this 0.930090372347188. It will always be a floating point number between 0 and 1.

Go through the instructions carefully especially #5, and if you’re still having trouble, post all of your code, and someone will be able to help you along.

Hi @midlindner, thanks for getting back to me. The ‘return’ is there as a return value right… this is the same code that’s there in the Get Help video on this exercise as well

Hi @yizuhi, if I were to not use the return, then what would I put in to return the value?

Yeah, but I don’t think you need to return the value in your second line of code.


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

The second time you use return, that’s good. But on the first line of your function, you should just be assigning a new value to userInput, so I don’t think it makes sense to use return right there.

1 Like

Thank you @yizuhi ! I tried that and it worked :slight_smile:

You’re welcome :relaxed: