8. what if choice1 is scissors


#1

hii

can you please tell me why is this code not working?

var userChoice=prompt("Do you choose rock,paper or scissors?");
var computerChoice=Math.random();
console.log(computerChoice);
if (computerChoice < 0.33)
{
computerChoice="rock";
}
else if (computerChoice > 0.33 && <= 0.67)
{
computerChoice="paper";
}
else
{
computerChoice="scissors";
}


8.What if choice1 is "scissors".Please HELP!
#2

else if (computerChoice > 0.33 && <= 0.67) // wrong

else if (computerChoice > 0.33 && computerChoice <= 0.67) // right

but you dont need it.you can simply write

else if (computerChoice <= 0.67)

because you have already declared

if (computerChoice < 0.33)

#3

Thanks for useful input.

But I have a small doubt, If i declare like this

if(computerChoice < 0.33)
{
-------------------}
else if (computerChoice <=0.67)
{


}

what will be the computerChoice when the value is 0.28888 as it is less than 0.33 and 0.67.


#4

certainly first condition.you can check it.

computerChoice=Math.random();

console.log(computerChoice);

if(computerChoice < 0.33)
{
console.log("ok")
}
else if (computerChoice <=0.67)
{
  console.log("not ok")
}
else
{
console.log("something else")
}

#5

Thanks.Now i understood how it works.Please help me with this.My code is working but it doesn't return anything like rock wins or scissors wins or paper wins.It simply displays the computerChoice.

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!";
        }
    }
    console.log(compare(userChoice, computerChoice));
    }

#6

after closing the function you should call it.so do like....

 return "scissors wins!";
        }
    }
    }
console.log(compare(userChoice, computerChoice));

#7

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