Ok I really felt like I did this right, but that's not true. Would love to know the issue with my code! Thanks!


#1

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 === "scissors") {

if (choice2 === "paper") {

    return ("scissors wins");
}

else {
    return ("paper wins");
}

}

else if (choice1 === "paper") {

if (choice2 === "rock") {

    return ("paper wins")
}

else {
    return ("scissors wins")
}

}

}
console.log (userChoice,computerChoice);


#2

You should log result of compare() function.

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

#3

arkussss thank you for the speedy response! I tried what you suggested but I still get this error...
(Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and rock)

This is what is being printed in console when I input "paper" in prompt box:

Computer: scissors
scissors wins


#4

Can you paste link to the exercise?


#5

@arkussss here is the link (i hope) https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/4?curriculum_id=506324b3a7dffd00020bf661#


#6

I don't know if it is a bug or something, but it seems that you are failing, because you have done too much :stuck_out_tongue:

I think you understand the code you wrote. Please try to pass this exercise with function like this:

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";
    }
}

I am curious if it will help you :smiley:


#7

@arkussss thank you for your assistance, I will give this a shot with your recommendation, and I agree as somewhat of a newbie to javascript I do understand the code. I will let you know how it goes. Thanks!


#8

@arkussss Thank YOU! The way you wrote the code was much more clear too me and I was getting confused with the syntax provided within the exercise itself. I have always been taught to simplify code, but it is harder to do while I am actually learning a new language.

For clarification (and to help anyone else who is a more visual learner) here is the completed and correct code for this particular exercise:

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 === "rock") return "paper wins";
    else return "scissors wins";
}

    else if(choice1 === "scissors"){
    if(choice2 === "paper") return "scissors wins";
    else return "rock wins";
}

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


#9

No problem, but honestly, I think that I should use more brackets in that code. I hardly ever use if's without curly brackets :stuck_out_tongue:


#10

Haha, it seems you're familiar enough with the code to know what the terminal will accept even if it isn't the "standard".

Nonetheless, thank you for the great help. I'll keep in mind to use those curly braces in the future, but if I ever take this learning and put it into something more practical and end up getting lazy I will remember that the code will be feasible even without using them! :smiley:


#11

The way to be able to read code like that is to format it properly, as you always should. Run it through JSBeautify or some such if you don't know what it should be, possibly even apply the differences manually for practice.

Here is also a more sane approach, albeit not what the instructions ask for:

function compare(a, b) {
    whoBeatsWho = {
        paper: new Set(['rock', 'spock']),
        rock: new Set(['scissors', 'lizard']),
        scissors: new Set(['paper', 'lizard']),
        spock: new Set(['rock', 'scissors']),
        lizard: new Set(['paper', 'spock'])
    }
    // check that a and b are valid choices
    if (!(whoBeatsWho.hasOwnProperty(a) && whoBeatsWho.hasOwnProperty(b)))
        return 'Somebody is cheating..';

    if (a === b)
        return 'The result is a tie!';
    if (whoBeatsWho[a].has(b))
        return a + ' wins';
    return b + ' wins';
}

#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.