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

487 points
549837239113cbef1c006d74_939648936
Submitted by
Matthew
about 4 years ago

8/9 Javascript: Build, Paper, Scissors! Help!

I have no clue what it is asking me to do on 8/9. Can you please tell me what i need to do/write or what code to add and where to add it!?! Please help! Here is my code: I haven't changed it at all since 7/9. Thanks.

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 a tie!"; }
else if(choice1 === "paper") {
if(choice2 === "rock") {
return "paper wins"; }
}
else {
return "scissors wins"; }
};

That code still works fine! I just need to know what to add/do with it!)


8 votes

permalink

@Matt1229,

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 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!"
"paper wins"
"scissors wins"
"rock wins"

//==========================================//

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 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
     };

3701 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
about 4 years ago


8 votes

permalink

@Matt1229,

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)

3701 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
about 4 years ago

1 Comment

549837239113cbef1c006d74_939648936 Matthew about 4 years ago

This helped! Thanks a bunch. I didnt realize that i had to use another else if instead of else!


8 votes

permalink

@Matt1229,

+++++++ executing the compare function ++++++++++++++

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

Now you want to execute the compare function,
as the compare function has 2 parameters
you have to provide it 2 arguments
which in our case that 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 into 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

3701 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
about 4 years ago

2 Comments

548ae0df9113cbf4c4001415_146633153 Lac Cristian about 4 years ago

Thanks it helped a lot :) !

18695113fee131ced1d753c5a264b62a?s=140&d=retro joetyna almost 4 years ago

thats great