No matter what I input in for userChoice the result is always ‘The game is a tie’ even when the computerChoice is different from the userChoice. Can someone let me know if my code is correct?
In the first statement, you pass arguments to the determineWinner function. The function returns a string, but you are NEITHER assigning the returned string to a variable NOR printing the result to the console. So, the result is discarded since you aren’t doing anything meaningful with the result.
In the second statement, you make a fresh call to determineWinner function but don’t provide any arguments. Lack of any arguments means that during execution of the function, both the userChoice and computerChoiceparameters are undefined. The if condition (userChoice === computerChoice) evaluates as true and the string "This game is a tie" is returned.
Ah, that makes sense. Somehow I was thinking that I called the function determineWinner(userChoice, computerChoice); so when I logged it in the console I didn’t have to put in the parameters again. I guess they’re separate.
Thanks
userChoice and computerChoice are parameters, whereas in a function call such as
determineWinner(userChoice, computerChoice);
userChoice and computerChoice are arguments.
Arguments are the values that you provide to the function.
Parameters are placeholders and allow your function the abstraction and flexibility to handle different inputs(arguments) without being re-written or modified. In your code, you have assigned "rock" and another random string to the userChoice and computerChoice variables, respectively. But, there is no compulsion that your arguments must have the same name as the parameters.
For example, all of the following are valid:
const x = "rock";
const y = "paper";
console.log(determineWinner(x, y));
// The arguments have been assigned to variables x and y.
// determineWinner will simply assign the first argument to the first parameter,
// and the second argument to the second parameter.
console.log(determineWinner("scissors", "paper"));
// Arguments haven't been assigned to any variables.
// When function is executed, the arguments will be assigned to the parameters.
const userChoice = "rock";
const computerChoice = "paper";
console.log(determineWinner(computerChoice, userChoice));
// Since the function definition is:
// const determineWinner = (userChoice, computerChoice) => {
// so "paper" will be assigned to userChoice parameter and
// "rock" will be assigned to computerChoice parameter (and not vice versa).
// Position of the arguments decides which argument is assigned to which parameter.
Thanks. I’m still trying to understand the last point. So if the parameters are userChoice and computerChoice respectively, when we pass the arguments in the function call, it will only regonise const userChoice = ‘rock’ as the first given parameter.
I probably chose a confusing example. Perhaps, this may be slightly clearer,
// Function definition
// name is first parameter, city is second parameter
const greeting = (name, city) => {
return `Hello! Your name is ${name} and you live in ${city}.`
}
// Outside the function, declaring and assigning values to
// some variables. Even though the variable names are same
// as the parameter names, they are not related to each other.
const name = "John";
const city = "Paris";
// Function call is made with the above as arguments
console.log(greeting(city, name))
// Output:
"Hello! Your name is Paris and you live in John."
The function doesn’t care what variable names have been used for the arguments outside the function. The first argument will be assigned to the first parameter, and the second argument to the second parameter. Position is important.
Since I used city as the first argument, its value i.e. "Paris" was assigned to the first parameter of the function i.e. name. name was the second argument, so its value i.e. "John" was assigned to the second parameter i.e. city.
Hence, the gibberish output:
“Hello! Your name is Paris and you live in John.”
The responsibility of passing arguments in the correct order lies with the programmer. The function doesn’t care about what variable names have been used outside the function. It isn’t going to try and match those variable names to the parameter names.
This is much clearer now. I also ran some code tests from my end with an example very similar to yours. I now get a better picture of parameters vs arguments. Thanks for explaining. I’m still new to this concept