0

So I'm making a Javascript program for "rock paper scissor lizard Spock" and I'm using Math.random(). I need to use if/else/else if's. Can somebody help me with the inequalities? I know what I currently have is not quite right:

   var userChoice = prompt("Do you choose Rock, Paper, Scissors, Lizard or Spock?");
var computerChoice = Math.random();
if (computerChoice > 0.20) {
    computerChoice = "rock";
} else if(computerChoice < 0.40 && >=0.20) {
    computerChoice = "paper";
} else if(computerChoice <0.60 && >=0.40 {
    computerChoice = "scissors";
}else if(computerChoice <0.80 && =>0.60 {
    computerChoice = "lizard";
}else {
    computerChoice = "Spock";
}
1
  • Hi Welcome to SO. In general you should to tell us the error, the error message, so as we can help quickly. How did you spot that your code is wrong ? Commented Apr 11, 2015 at 7:12

2 Answers 2

1

You need both a left and a right operand in each of the conditionals. That is:

else if(computerChoice < 0.40 && computerChoice >=0.20) {
Sign up to request clarification or add additional context in comments.

Comments

1

I'm not familiair with the game, so I don't know what the winning order is. But this could help you. It's just assingning numbers to the words and compares the numbers.

I've made a little fiddle

var userChoice = prompt("Do you choose Rock, Paper, Scissors, Lizard or Spock?");

var test = {
    Rock: 0,
    Paper: 1,
    Scissors: 2,
    Lizard: 3,
    Spock: 4
};

userChoice = test[userChoice]; //a number from 0 - 4

var computerChoice = Math.floor(Math.random() * 5); //(gives number from 0 to 4)

if (userChoice == computerChoice) {
    alert('it\'s a tie');
} else if (userChoice > computerChoice) {
    alert('user wins');
} else {
    alert('computer wins');
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.