Rock, Paper, Scissors Exercise Questions

Hi Everyone!

I am currently doing the JavaScript program and I am at the Rock,Paper,Scissors exercise.

I have a couple of questions.

  1. How do we know what the user has put in? I understand where getComputerChoice comes from since we create a switch statement and combine it with Math.random(). But I just dont understand where we get the UserChoice from. We have put a bunch of conditionals on what should happen if the user inputs various answers but nowhere do we put a input field or randomization on the answer that they give.

  2. Why do we not give getComputerChoice a parameter?

  3. In the switch I used both return and break in my solution but the guy in the instructional video only uses return. Which one should we use and why does it matter?

I appreciate any help ! thanks!

So I’ll take them one step at a time and hopefully I can answer your questions!

  1. As this is relatively early in the learning process there’s a lot that has been left out of it being a completely functional program. For the moment, the idea is that whatever is input in the final function call is the users input, as the user is whoever is running the program. In general Javascript is used in web development, and so the idea is that eventually this would be the back of a website, where the user input would be retrieved from said website. However this is purely the JS course and as such doesn’t cover that, therefore it’s not there yet. However these are all things that you can add on yourself later in your coding journey, and it’s good that you’re asking these kinds of questions. It’s important to be able to see how you can improve your software and where the next scope for improvement would be.

  2. I suppose the question for you to answer there is what parameter would you give to giveComputerChoice()? A function takes in a parameter when it’s used for something, but what would we need to take in to make the function work? The idea is that this is the computers choice, which is just a random number generator, and as such there’s no specific reason to actually pass anything in, as from a user standpoint nothing extra is needed to make the function carry out it’s, well, function.

  3. It’s a question of what you want to do. Return immediately exits the function, whilst break immediately exist the switch statement. So if the switch is the last part of the function, and the result of the switch is to be returned, then just using return is fine as it saves any extra checks. The actual performance impact is negligible unless you get to a huge switch statement which should never really happen, so it’s function is more for clean code (which is oftentimes personal preference). However, if there is more code within the function to follow the switch statement, then you would want to use break, as returning would exit the function prematurely.

Hopefully this answered your questions, anything else do let me know!

1 Like

Yes it did! thanks alot for taking the time to answer =).