Help me to find correct code


#1

what is wrong in my code below
var computerChoice = Math.random();
console.log(computerChoice);
if (computerChoice<=0&&>= 0.33)
{
computerChoice=“rock”;
}
else if(computerChoice<=0.33&&>=0.66)
{
computerChoice=“paper”
}
else
{
computerChoice=“scissor”
}


#2

i too corrected the semicolons which i missed in the above code


#3

So there are 2 main issues with your code, 1 would cause an error, the other would just give you an incorrect result.

  1. if(computerChoice<=0&&>=0.33) is invalid. you have to specify the variable again that line should look like this:
    if(computerChoice<=0 && computerChoice>=0.33)
    the else if statement will need to be fixed as well

  2. With the if and else if statements fixed your code should run but it will give you the wrong result
    let’s pretend computerChoice = 0.15
    if(computerChoice<=0 && computerChoice>=0.33) would be if(0.15<=0 && 0.15>=0.33)
    no number will ever pass that, it can’t be less than 0 and greater than 0.33, those operators need to be flipped.
    if(computerChoice >= 0 && computerChoice <=0.33)
    the same thing needs to be done to the else if statement


#4

thanks sir for guiding me to take the correct code


#5

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