I have a couple of questions.
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.
Why do we not give getComputerChoice a parameter?
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!
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.
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!
Yes it did! thanks alot for taking the time to answer =).
This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.