9/9 How does JS interpret userChoice as choice1?


#1

In function "compare" there are two parameters "userChoice" and "computerChoice".

When the if-condition is set inside the function compare, how can it interpret my userChoice as being choice1? Why aren't userChoice and choice1 independent?

Given that variable userChoice and computerChoice is declared...

var compare = function(userChoice, computerChoice) {
   if(choice1 === choice2) {
   return "Tie!";

   } else   if(choice1 === "rock") {
            if(choice2 === "scissors") {
            return "You won!";
            } else {
            return "The computer won!";
            }

   } else   if(choice1 === "paper") {
            if(choice2 === "rock") {
            return "You won!";
            } else {
            return "The computer won!";
            }

   } else   if(choice1 === "scissors") {
            if(choice2 === "rock") {
            return "The computer won";
            } else {
            return "You won!";
            }
    }   
 };

My intial thought were that if the if condition were able to read my userChoice as choice1, I should convert choice1 to userChoice.

var compare = function(userChoice, computerChoice) {
choice1 = userChoice;
choice2 = computerChoice;

    if(choice1 === choice2) {
    return "The result is a tie! Try again!";
    } //rest of code...
};

In other examples,the parameters "choice1, choice2" are set. but there are still declared variables for "userChoice" and "computerChoice".

var compare = function(choice1, choice2) {

    if(choice1 === choice2) {
    return "The result is a tie! Try again!";
    } //rest of code...
};

My experience tells me that, without these convertions, I would not get the correct results.

Maybe I didn't understand the instruction
02: "We need to use the variables userChoice and computerChoice." and
03: "Call your function and pass in userChoice and computerChoice as your two arguments."

I appreciate your help.


#2

Long Post WARNING!!!


Sir, While I understand where you were attempting to go with this code it's a little awkward and inefficient.

I would like to start with this line of code, and how it can be replaced with example 1.

Your Code:
var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
    return "The result is a tie! Try again!";
    } //rest of code...
};

Example 1:

function compare(choice1,choice2) {...}

This is because you're passing the function compare a couple parameters. Essentially your way would work as well. However it's most efficient / clean to have function compare() {}

***NOT A NECESSARY FIX BUT A SUGGESTED ONE***

Earlier in the exercises preceding this one you should have prompted the user for their choice of rock, paper, or scissors as the first line. And as the second line the computerChoice should have been randomized.

Mandatory fix point follows:

Your Code:
var compare = function(userChoice, computerChoice) {...}

Remember, this is the PARAMETERS that you are passing the function. You would not use predetermined variables for this.

Example 2:

var compare = function(choice1, choice2) {...}

After the function you would pass the function the parameters with your predetermined variables as follows.

compare(userChoice, computerChoice);

This would take the variables and put them into the function as the parameters (choice1, choice2) in the function "compare"

The only fixes that need to be done is to take your original code

and change "userChoice / computerChoice" to choice1, choice2 respectively.

Then add

compare(userChoice,computerChoice);

I hope this helps.

Your friendly neighborhood coder -LoLMaN


#3

Thank you, LolMaN. When I went to sleep, I immediately realised what was wrong with my code. It got even clearer with your answer. Now I got my brain ordered. I think the reason why I had this problem was that I didn't properly understand what it meant to "pass in userChoice and comperChoice as your two arguments.", while the parameter is kind of a placeholder for those. Instead, I changed the parametres as (userChoice, computerChoice), because I thought I was passing those in as arguments, but instead, changing the parametres.

So this is how it works:

  1. I declare two variables, userChoice and computerChoice.

  2. Then I make a function which will compare two arguments, now given placeholders as parametres (choice1, choice2), or more technical (a,b)

  3. Meanwhile, I go ahead and tell what will happen when arguments are passed in the function.

  4. Then I pass the declared variables as arguments in the function by writing "compare(userChoice, computerChoice)", as if userChoice-argument took place of parameter choice1 (or a), and computerChoice-argument took place of parameter choice2 (or b).

So, simplified:

var argument1 = //string;
var argument2 = //string;
function alpha(parameter1,parameter2) {//consequence};
alpha(argument1,argument2);

#4

Absolutely correct. Good explaination


#5

I'm sorry but I don't get it. I get the whole if/else concept, and I've got all the combinations of rock, paper, scissors, but I am completely stymied when it comes to step 3, which is "Call your functions and pass in userChoice and computerChoice as your two arguments."

Thanks!


#6

Hi, micromaster54926. In order to help you better, I am interested to know what is with the step 3 you get completely stymied.

Do you remember calculating the value of an orange using functions?. You would have to "insert" the orange value (argument) in the function construction in order to get your result.

Step 3 means "insert your variables in to the function".
Doing that executes the function, and will print your results.

Here, I "insert" my variable (now as an argument) userChoice and computerChoice by "replacing" the parameter which construct the function and its following consequences (if/else, using parametres choice1, choice2, etc...) all this by writing a new line. What I mean with "replace", is that I do not alter the function code with its precious parameter, leave that alone. You have to "insert" your arguments like this:

compare(userChoice, computerChoice);

Given your function is called "compare".

construction:

   function compare(choice1, choice2) {if, else...};

insertion/execution

   compare(userChoice, computerChoice);