This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Build "Rock, Paper, Scissors" Forum

View Course

41 points
521da5e5c7f906b876681a143c1251e0?s=140&d=retro
Submitted by
Emory Monroe
almost 2 years ago

8/9, need some help?

So I got everything up to the last part when it comes to calling your function. I guess I am just not sure what it means by saying to use userChoice and computerChoice as the arguments.

If someone could just explain what it means and how I would do that, it'd be wonderful. Thank you in advance.


3 votes

permalink

the FUNCTION talk

var myFunc = function( param1, param2) {
       //Begin of  anonymous FUNCTION-BODY
       //VARIABLE -myFunc- has an -anonymous function- assigned
       //this -anonymous function- has 2 PARAMETERS param1 and param2
       //param1 and param2 PARAMETERS are used 
       //as -local- VARIABLES throughout the FUNCTION-BODY

      console.log( param1 + " and " + param2 ) ;

      //End of anonymous FUNCTION-BODY
};

If you want to call/execute the anonymous function
you will have to add a pair of parentheses to the variable myFunc
like
myFunc();
As the anonymous function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren");

some quotes from the outer-world:

argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block

OR

"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".

3311 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
almost 2 years ago


2 votes

permalink

++++ function with 1 parameter using return-statement

var myFunction = function( param1 ) {
       //Begin of FUNCTION-BODY
       //myFunction =function= has 1 PARAMETER param1
       //this param1 PARAMETER is used as a -local- VARIABLE
       //throughout the FUNCTION-BODY

      return param1;

      //End of FUNCTION-BODY
      };

you have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 );

some quotes from the outer-world:

argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block

OR

"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the console.log()-method to do a display.

var theResult = myFunction( 4 );
console.log( theResult );

OR directly

console.log( myFunction( 4 ) );

3311 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
almost 2 years ago


2 votes

permalink

As a little extra help:

Maybe if you create a truth-table it is easier to understand.
You will look out of perspective og choice1:

                  | ch-2  |  ch-2  | ch-2
                  | rock  |  paper | scissors
  ----------------|-------|--------|----------
                  |       | paper  |  rock
 choice1     rock |   X   | wins   |  wins    (first else if )
                  |       |        |
  ----------------|-------|--------|----------
                  | paper |        |scissors
 choice1    paper | wins  |    X   |  wins   (second else if )
                  |       |        |
 -----------------|-------|--------|----------
                  |  rock |scissors|
 choice1 scissors |  wins | wins   |   X     (third else if )
                  |       |        |
 -----------------|-------|--------|----------

We have to evaluate 9 possibilities:
In written form:

 if choice1 equals choice2 then "The result is a tie!"    (all the X'ses)

else if choice1 is "rock"? Given choice1 is "rock",
a. if choice2 === "scissors", then "rock" wins.
b. if choice2 === "paper", then "paper" wins. (use else NO condition)

else if choice1 is "paper"? Given choice1 is "paper",
a. if choice2 === "rock", then "paper" wins.
b. if choice2 === "scissors", then "scissors" wins. (use else NO condition)

else if choice1 is "scissors"? Given choice1 is "scissors",
a. if choice2 === "rock", then "rock" wins.
b. if choice2 === "paper", then "scissors" wins. (use else NO condition)

3311 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
almost 2 years ago


1 vote

permalink

the creation of your compare function
/ Parameter vs. Argument /
You have to create a function compare which takes 2 parameters.
parameter1 is choice1
parameter2 is choice2
throughout your function you will use these parameters as variables.
(for instance you are checking if the parameter1 being _choice1_ equals "paper")

as IF / ELSE-IF condition you will have to use
(choice1 === "rock") , (choice1 === "paper") , (choice1 === "scissors")
(choice2 === "rock") , (choice2 === "paper") , (choice2 === "scissors")
You have to return one of the following strings:
"The result is a tie!" <==== with an exlamation-mark !!!
"paper wins"
"scissors wins"
"rock wins"

3311 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
almost 2 years ago


1 vote

permalink

Please have a look at the following skeleton...
You are getting lost in the curly-bracket-{-} Forest
You will have to use following IF ELSE-IF statement construction
for every section a step further:

The curly-brackets-{ } are used to enclose code-blocks
pieces of code that stand together...

The if class of statements should have the following form:

 var compare = function(choice1,choice2) {
      //Begin of FUNCTION-BODY
     if (condition) {
             //IF code-block with it's statements
     }

     else if (condition) {
              //ELSE-IF  code-block with it's statements
           if (condition) {
              //IF  code-block with it's statements
           } 
           else {
              //ELSE  code-block with it's statements
          }
     } 

     else if (condition) {
           if (condition) {
               //statements
           }
           else {
              //statements
          }
     } 

     else if (condition) {
           if (condition) {
              // statements
           } 
           else {
              // statements
          }
     } 

      //End of FUNCTION-BODY
     };

3311 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
almost 2 years ago


1 vote

permalink

================================

Now you want to execute the compare function,
as the compare function expects 2 parameters
you have to provide it 2 arguments
which in our case would be userChoice and computerChoice.
compare(userChoice,computerChoice);

some quotes from the outer-world:

argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block

OR

"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".

================================

As you are using the return-statement in your compare function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the console.log()-method to do a display.

var theResult = compare(userChoice,computerChoice);
console.log( theResult );

OR directly

console.log( compare(userChoice,computerChoice) );

P.S.
I would put an extra console.log( userChoice ); just under the computerChoice-display

google search
== the Book ==
javascript [your question] site:developer.mozilla.org

== discussions / opinions ==
javascript this explained site:stackoverflow.com
http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work
== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript

3311 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
almost 2 years ago


0 votes

permalink

hey!
at the end write this - compare(userChoice, computerChoice)

744 points
558814859113cb591f000163_937526056
Submitted by
Mohamed Arif
almost 2 years ago

1 Comment

5334fc819c4e9db930001a0d_32139218 Leon almost 2 years ago

Even better console.log( compare(userChoice, computerChoice) );


0 votes

permalink

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);
var compare = function(choice1, choice2) {
if(choice1 === choice2) {
return "The result is paper";
}
};

749 points
542ef04c80ff33a2120004a1_813377016
Submitted by
Stop leaving your account signed in.
over 1 year ago

1 Comment

5334fc819c4e9db930001a0d_32139218 Leon over 1 year ago

You have to return one of the following strings:
"The result is a tie!" <==== with an exlamation-mark !!!
"paper wins"
"scissors wins"
"rock wins"