Computer always chooses scissors or rope. Why?


#1


I know it must be something simple, but I'm stuck. After I added rope to the exercise, the computer seems to be choosing only scissors/rope option, no matter what. Any ideas what I'm doing wrong?
Here's a JFiddle link: https://jsfiddle.net/nw32xxja/

var Game = function() {

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


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


    var compare = function(userChoice, computerChoice) {

      if (userChoice === computerChoice) {
        return "The result is a tie!";
      }


      if (userChoice === "scissors") {
        if (computerChoice === "paper") {
          return "User: scissors, PC: paper. You win.";
        }
        if (computerChoice === "rope") {
          return "User: scissors, PC: rope. You win.";
        } else {
          return "User: scissors, PC: rock. PC wins.";
        }

      }

      if (userChoice === "paper") {
        if (computerChoice === "rock") {
          return "User: paper, PC: rock. You win.";
        }
        if (computerChoice === "rope") {
          return "User: paper, PC: rope. PC wins.";
        } else {
          return "User: paper, PC: scissors. PC wins.";
        }
      }

      if (userChoice === "rock") {
        if (computerChoice === "paper") {
          return "User: rock, PC: paper. PC wins.";
        }
        if (computerChoice === "rope") {
          return "User: rock, PC: rope. PC wins.";
        } else {
          return "User: rock, PC: scissors. You win.";
        }
      }

      if (userChoice === "rope") {
        if (computerChoice === "paper") {
          return "User: rope, PC: paper. You win.";
        }
        if (computerChoice === "rock") {
          return "User: rope, PC: rock. You win.";
        } else {
          return "User: rope, PC: scissors. PC wins.";
        }
      } else {
        return "Wrong choice! Try again."
      }
    };


    var result = compare(userChoice, computerChoice);
    document.getElementById("result1").innerHTML = result;

  }


#2

I think it has something to do with the if statements. delete the numbers that I put arrows to and see if it works


#3

you also forgot the semi colon at the end of the return


#4

Since this is obviously a spin off effort, it is no longer Q&A material for the lesson module. It is moved to the Corner Bar for free discussion.


#5

It's always going to do one of those, either rope, or scissors, that's what it says (either this, otherwise that), so they'll override whatever value computerChoice was previously assigned to

Further,

Is evaluated in this order:

(0.24 <= computerChoice) < 0.5

Where the part in the expression evaluates to either true, or false

true < 0.5

false < 0.5

In that context true/false will behave like 1 and 0 respectively and that probably doesn't match what you meant to describe


#6

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