How does Javascript know that "choice1" is the same as "userChoice"?


My code works, but I just have a question about why it works.

How does userChoice become choice1 and computerChoice become choice2?
I would think that I would have to define them as such, so I must be missing something.

Is it from the code: console.log(userChoice, computerChoice)? And if so I'm still confused as to how choice1 and choice2 are known to represent those two arguments.

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: " + computerChoice);
console.log(userChoice, 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 {
            return "paper wins";
    else if(choice1 === "paper") {
        if(choice2 === "rock") {
            return "paper wins";
        else {
            return "scissors wins";


add a function call at the very end of your code:


the arguments you provide at your function call are then copied in the parameters (choice1 and choice2) this is how javascript knows that userChoice is choice1

see parameters as placeholders, they don't have a value until you provide arguments at the function call

we can even code something like this:

var choice1;
var choice2;
var userChoice = "paper"
var computerChoice = "rock"
choice1 = userChoice
choice2 = computerChoice

except in case of function, the arguments are assigned to the parameter by js.


Greetings. As I can see you didn't paste the entire code, you missed :
Line 39.- compare(userChoice, computerChoice);

There's where you call your function "compare" and pass as 'choice1' the 'userChoice' parameter, same as for 'choice2' with 'computerChoice'.

The function does what it haves programmed inside and returns the result as stated.
I hope I cleared your question.
It's late night here , have a good one.
Keep programming !


if you have a function f(a, b) and you do f(3, 9) then a gets assigned 3 and b 9


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.