Function not returning string value (Lesson 9)


#1

Hello everyone! I am enjoying these lessons, they're great. One complaint I have is that I don't think the lesson checks if your code is actually working properly or not before letting you move on. I say this because I know mine is not working ... but it says I still completed the course!

I am not content with moving on to the next lesson until I get this to work. Unfortunately I've done just about everything I can think of. Here is my code.

// Declare variable equal to the users choice of rock paper or scissors.
var userChoice = prompt("Do you choose rock, paper or scissors?");
console.log("Your choice: " + choice1);

//Declare variable to find computers random choice.
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer's choice: " + computerChoice);

//Compare choices to see who wins.
var compare = function(choice1, choice2) {
    var choice1 = userChoice
    var choice2 = computerChoice
    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";
        }
    } else {
        if(choice2 === "paper") {
            return "scissors wins"
        } else {
            return "rock wins"
        }
    }
};

//Display the results.
console.log(compare)

This results in the following output:
Your choice: rock, paper, or scissors
Computer's choice: rock, paper, or scissors
[Function]

It just says [Function] instead if printing the results returned in the function. I tried a lot of thins, such as declaring local variables and setting the perimeters to equal the variables used to find the user and computer's choice before the if/else statements trigger. I don't understand what is going on.

Any help?


#2

Well, the first problem I can see is on this line:

console.log(compare);

You're just shooting the function into a console.log statement and it's all like... "Ok... that's... that's a function... I'm going to put function.. yeah that's what I'm going to do!"

It has no idea that you're trying to get it to console.log the return statement!

I'll show you how to fix this in just a moment!


#3

Yeah alright so here's how you fix it.

remember compare is a function and it's asking for two parameters (choice1, choice2) so when you want to get the result from that you have to give it a couple parameters to use!

so:

console.log(compare(computerChoice, userChoice)); // as choice1, and choice2 respectively

#4

Oh man, I forgot all about syntax regarding plugging in your inputs for perimeters. Thank you so much for the solution! Funny how little slips like that cause the entire thing to fall apart.