This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by Matthew
over 9 years

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!)

Answer 54977fe151b887e04e0060f9

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
     };
points
Submitted by Leon
over 9 years

Answer 54978014e39efe3f19006243

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)

points
Submitted by Leon
over 9 years

1 comments

Matthew over 9 years

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

Answer 549780e8d3292f345900606a

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

points
Submitted by Leon
over 9 years

2 comments

Lac Cristian over 9 years

Thanks it helped a lot :) !

joetyna about 9 years

thats great