1

I'm trying to learn javascript and this lesson is having me create a rock, paper, scissors game. It is asking for me to:

Call your function and pass in userChoice and computerChoice as your two arguments.

I am not sure how to pass in userChoice and computer Choice. I tried doing this:

console.log(compare + userChoice + computerChoice)

But obviously it isn't correct.

What am I doing wrong??

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 === "rock") {
     if (choice2 === "scissors") {
         return "rock wins";
     } else {
         "paper wins";
     }
 } else if (choice1 === "paper") {
     if (choice2 === "rock") {
         return "paper wins";
     } else {
         return "scissors wins";
     }
 } else if (choice1 === "scissors") {
     if (choice2 === "rock") {
         return "rock wins";
     } else {
         return "scissors wins";
     }
 }
}
console.log(compare + userChoice + computerChoice)
Becky
  • 2,283
  • 2
  • 23
  • 50
  • substitute userChoice and computerChoice for choice1 and choice2 in the line: var compare = function(choice1, choice2) – Cindy Meister Nov 05 '15 at 16:38
  • I get this as the result after doing it? Computer: paper undefined....undefined being after paper, like my answer wasn't defined. – Becky Nov 05 '15 at 16:42

1 Answers1

1

You're VERY close, arguments are passed to functions like this:

compare(userchoice, computerChoice);

OliverRadini
  • 6,238
  • 1
  • 21
  • 46
  • Thanks! Quick side not question. With every lesson in this I have not put a semi colon with `console.log`'s... this system hasn't flagged anything. Is it needed in real life applications? – Becky Nov 05 '15 at 16:38
  • 1
    It's a fairly in-depth topic, but generally speaking it's best to use them in most situations. This is because if you don't use them, they are sometimes inserted automatically, and that can cause confusion. You can read more about it here: http://stackoverflow.com/questions/8108903/are-semicolons-mandatory-in-javascript-statements – OliverRadini Nov 05 '15 at 16:44
  • If I enter rock, I get an undefined answer? Is something wrong with how I coded it? – Becky Nov 05 '15 at 16:51
  • 1
    There is one small problem. I'm sure you can figure it out if you like at line 17 in the JSFiddle I linked above (jsfiddle.net/pd35eqkv), especially if you consider that the choice "rock" works if it wins, but not if it loses. Hope that helps. – OliverRadini Nov 05 '15 at 16:59
  • Wow..yes it did. I can't believe I forgot the `return`...Thanks for all of your help!! – Becky Nov 05 '15 at 17:02