This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Shaimaa Hamdy
over 8 years

I need help

I cannot solve this one at all I don’t understand so I can’t solve this code

Answer 56011b629113cbd16e00004b

0 votes

Permalink

Sry but a little more information is needed could you post you’re code or explain the problem a little further? Also have a look at the FAQ thread maybe the problem is already covered here: https://www.codecademy.com/forum_questions/51fc4178f10c60f4eb001233

points
Submitted by haxor789
over 8 years

Answer 5606483451b8877f1400008a

0 votes

Permalink

الكود ده اشتغل معايا…this is the completed code

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

Edit: formatting fixed by haxor789

points
Submitted by Adam Smith
over 8 years

2 comments

Shaimaa Hamdy over 8 years

it didn’t work

haxor789 over 8 years

@Adam The general structure of the if and else if case is alright, just a bit confusing that you start with the scissors case when the exercises goes through it in rock, paper, scissors order but that is your choice. What is wrong here is that you added a default rock wins for any other cases. That might be suitable as a placeholder but it would be necessary to replace it with the other two cases :) @Shaimaa Hamdy again it would really help if you could tell what exercise you’re working on and what code you’ve already written.

Answer 560979a7e39efe6ca800026e

0 votes

Permalink

@haxor789 this is my code : : : : :

{
    return "The result is a tie!"
} else if (choice1 === "scissors") {
    if (choice2 === "rock") {
        return "rock wins"
    } else if (choice2 === "paper") {
        return "scissors wins";
    }
} else {
    return "rock wins";
}
};
compare(userChoice, computerChoice);
points
Submitted by Shaimaa Hamdy
over 8 years

Answer 5609ad2ae39efe0927000084

0 votes

Permalink

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!"
    }
    else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins"
        } else if (choice2 === "paper") {
            return "scissors wins";
        }
    }
...
}

As said this part of the code is correct but why do you both try to go on with:

 else {
    return "rock wins";
}

this? This means if it is not a tie and choice1 is not scissors the result will always be “rock wins!”. What you need to do instead here is add 2 more cases dealing with “rock” and “paper” in the way you did for “scissors” e.g. add

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

and one more for “paper”. Then you have all cases covered: choice1 being choice2 and choice1 being “rock”,”paper” or “scissors” with all the possible choices for choice2. As said there is an FAQ thread dealing with the game logic as well. Hope it gets a bit clearer.

points
Submitted by haxor789
over 8 years