Next Steps


#1


https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/5?curriculum_id=506324b3a7dffd00020bf661


The code seems to be right but the winner is not announced. I tried to add a console.log command and it keeps showing "the result is a tie". Not sure what I'm 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 {
            return "paper wins!";
        }
    }
else if (choice1 === "paper") {
    if(choice2 === "scissors") {
        return "scissors wins!";
    }
    else {
        return "paper wins!";
        }
}
else if (choice1 === "scissors") {
    if (choice2 === "rock") {
    return "rock wins!";
    }
    else {
        return "scissors wins!";
    }
}
};
compare(userChoice, computerChoice);


#2

i don't see a problem:

(this is your code), after 2 rounds the computer didn't pick the same thing, and it seems to be fine, but it wouldn't hurt to wrap your function call inside a console.log, if you also want to announce that player or computer won, you might need to add such a string to the returns statement


#3

Thanks for the quick reply!

What I meant was, all I see is the Computer announcing but not the line below (i.e. "rock wins!")

When I add the additional line console.log(compare()); all I get is "the result is a tie!" no matter what the actual result should be


#4

wait, then you don't supply argument when you wrap the function call with console.log? It should be:

console.log(compare(userChoice, computerChoice));

#5

Now it just says "Undefined"


#6

i am running your code, and it works fine:

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 {
            return "paper wins!";
        }
    }
else if (choice1 === "paper") {
    if(choice2 === "scissors") {
        return "scissors wins!";
    }
    else {
        return "paper wins!";
        }
}
else if (choice1 === "scissors") {
    if (choice2 === "rock") {
    return "rock wins!";
    }
    else {
        return "scissors wins!";
    }
}
};
console.log(compare(userChoice, computerChoice));

i could upload another screen shot. I don't know why it says undefined, maybe your browser is sending cached data after so many requests, try a different browser


#7

Tried both

compare(userChoice, computerChoice);

which returns no winner

and:

console.log(compare(userChoice, computerChoice));

which returns "undefined". I also tried in a different browser and I get the same result. I guess I'll move on if I'm not making a mistake, but I'm wondering if this is happening to other people as well


#8

i give up, i put your code in a bin, do you have the same problem here?


#9

It works! Thanks, I was about to go insane


#10

I have no idea why it wasn't working in the exercise, but your code was good, so good to see it is resolved.