Hi,my code if fine but i dont understand it


#1

How does he know that the userChoice is choice1 ? when i havent defined it anywhere...

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer's choice: " + computerChoice);

var compare = function(choice1,choice2){
if(choice1 === choice2){
return "The result is a tie!" ;
}
else if(choice1 === "rock"){
if(choice2 === "scissors")
return "rock wins";
}
else if(choice1 === "paper"){
if(choice2 === "rock")
return "paper wins";
}
}
compare(userChoice,computerChoice)


#2

here:

compare(userChoice,computerChoice)

you call the function, in which you supply arguments (to satisfy choice1 and choice2 parameter), so the parameters are basically placeholders until you call the function.


#3

Actually, you did!
Take a look at your last line of code:
compare(userChoice,computerChoice) You called the function named compare() and your parameters replaced all the
choice1 & choice2. Basically Choice1 was replaced by userChoice & Choice2 was replaced with computerChoice

Hope this helped!


#4

i figured that out , but i dident figure out why "choice1,choice2" when i change parameter "choice1" to anything else for example "choooooice" it doesnt work.....how does he know that "choice1" is "userChoice and that "choice2" is "computerChoice" i havent defined that anywhere.... or is it me just being stupid.
P.S.
also if i change the name of the variable "userChoice" to "myChooooice" it still gives out error...


#5

The reason changing one of the parameters in var compare = function(choice1,choice2){ to anything
else doesn't work is because the rest of the code in your function relies on the proper spelling of your parameters.

Example:

var multiply = function(a,b) {
  console.log(a * b);
}
multiply(2,2);
//Returns 4;
//If you change it:
var multiply = function(a,z) {
  console.log(a * b);
}
multiply(2,2); 
//Now it doesn't understand what b is. (It has been replaced by z)

Hope this helps!


#6

yes,very much, thank you...


#7

You're welcome! Glad I could help :slight_smile:


#8