I completed the exercise but compare function results will not display


#1


/* Hi Everyone. 
 I am confused.  I have been playing with this for 24 hrs. 
 The code just prints the  user and computer choice
 but won't return the results of the winner. I even changed
 the choice1 and choice2 to reflect computerChoice
 and userChoice and still nothing prints. 
At the end I tried to console.log the compare function
 to see if it would print the results but it just prints [function].
 WHAT IS WRONG WITH MY CODE!!!!   ;(  
  Help me please!!!!!!!!!!  */  

var userChoice = prompt("Do you choose rock, paper or scissors?"); console.log (userChoice); 
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log(computerChoice);
var choice1 = userChoice;
var choice2 = 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 
    {
    return "Paper wins!";
    }
 }
 else if (choice1 === "paper")
 {
    if (choice2 === "rock") {
    return "Paper Wins!";
    } else {
     return "Scissors wins!"
    }
 }     
}


#2

why do you do this:

var choice1 = userChoice;
var choice2 = computerChoice;

?

I would simply add a function call at the end (after the function) and pass userChoice and computerChoice as arguments, then they are copied into the function parameters.


#3

I'm new to this . what would that look like in code?


#4

# function deceleration with parameter
var example = function(par){
   console.log(par);
}

# function call + arguments
example("hello world")

except your function has 2 parameters, so you need 2 arguments. and in your case the arguments are variables, not strings.


#7

I just noticed my user name lol. I did not come up that name haha


#8

For clarity ...So if am calling the function compare
in this case would it look like this or am I totally off base? :slight_smile:
(BTW thanks for being patient with a senior citizen...LOL)

var result = compare ( userChoice, computerChoice ) {
console.log ( result );
}


#9

well the function call is most certainly in there:

compare ( userChoice, computerChoice )

there is a difference between the function declaration (creating the function):

# function deceleration with parameter
var example = function(par){
   console.log(par);
}

and the function call:

# function call + arguments
example("hello world")

you needed to add the function call:

compare ( userChoice, computerChoice )

a function call will make the function execute, does that help?

If not, can you post an updated version of your code? (full code), then i will help you further :slight_smile:


#10

Thanks sooooo much! I got confused on calling and declaring the function .
I added the call and it works great now!!!!
:wink:


#11

you're welcome :wink:

Well done!


#12