Solution for th 9/9 next steps


#1

this is my solution for the next steps I hope it could help.

var userChoice = prompt("Do you choose rock, paper, scissors or rope?");
var choice = function ()
{
userChoice = prompt("Do you choose rock, paper, scissors or rope ?");

};
var check = function ()
{
while(userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors" && userChoice !== "rope")
{ confirm(userChoice +" is invalid entry");
choice();
}
};
check();
//console.log("User: " + userChoice);

    var computerChoice = Math.random();
    if (computerChoice < 0.25)
    {
        computerChoice = "rock";
    }
    else if(computerChoice <= 0.5) 
    {
        computerChoice = "paper";
    } 
    else if(computerChoice <= 0.75) 
    {
        computerChoice = "scissors";
    } 
    else 
    { 
        computerChoice = "rope";
    }

console.log("User: " + userChoice);
console.log("Computer: " + computerChoice);

var compare = function (userChoise, computerChoice)
{
if(userChoise === computerChoice)
{
var result = prompt( "The result is a tie! Do you want to play again?");
if(result === "yes")
{
choice();
check();
}

}
else if(userChoise === "rock")
{
    if(computerChoice === "scissors")
    { 
        return "rock wins";
    }
    else if(computerChoise === "paper")
    {
        return "paper wins";
    }
    else
    {
        return "rock wins";
    }
}
 else if(userChoise === "paper")
{
    if(computerChoice === "rock")
    { 
        return "paper wins";
    }
    else if(computerChoise === "scissors")
    {
        return "scissors wins";
    }
    else 
    {
        return "rope wins";
    }
}
 else if(userChoice === "scissors")
 {
     if(computerChoice ==="rock")
    { 
        return "rock wins";
    }
    else if(computerChoice ==="paper")
    {
        return "scissors wins";
    }
    else
    {
        return "rope wins";
    }
 }
else 
 {
     if(computerChoice === "rock")
    { 
        return "rock wins";
    }
    else if(computerChoice === "paper")
    {
        return "rope wins";
    }
    else
    {
        return "rope wins";
    }
 }

};
compare(userChoice, computerChoice);


#2

Reread your code
as you are making a mess
userChoice and userChoise
computerChoice and computerChoise


#3

I have a correct execution ,which lines please.


#4

Hey, Here is the solution :grinning:

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

#6

@kshitij760,
Please read
http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b
and concentrate on

  • executing the compare function