Ishan Sahu
Ishan Sahu

Reputation: 121

Javascript string comparison sometimes give error, but when trying again it works fine

I am currently trying to make a basic rock, paper and scissor game using html,css and java script.The program works fine sometimes, but on certain occasions it does compare my string properly.Although if I enter the same string again, the program will give me the desired output.Can anyone suggest where am I going wrong? I have attached my project link for reference. currently I am only trying to display the output in console.

computerPlay = () => {
  const game = ["Rock", "Paper", "Scissor"];
  var result = game[Math.floor(Math.random() * 3)];

  return result;
}
var player = 0;
var computer = 0;
round = (playerSelection, computerSelection) => {
  computerSelection = computerPlay().toLowerCase();
  playerSelection = playerSelection.toLowerCase();
  if (
    (playerSelection == "paper" && computerSelection == "rock") ||
    (playerSelection == "rock" && computerSelection == "scissor") ||
    (playerSelection == "scissor" && computerSelection == "paper")
  ) {
    console.log("computer selection is \"" + computerSelection + "\"");
    console.log("player selection is \"" + playerSelection + "\"");
    console.log("\"" + playerSelection + "\" beats \"" + computerSelection + "\" hence player won");
    player++;
    console.log("player point is \"" + player + "\" ");
    console.log("computer points is \"" + computer + "\" ");

  } else if (
    (playerSelection == "rock" && computerSelection == "paper") ||
    (playerSelection == "scissor" && computerSelection == "rock") ||
    (playerSelection == "paper" && computerSelection == "scissor")
  ) {
    console.log("computer selection is \"" + computerSelection + "\" ");
    console.log("player selection is \"" + playerSelection + "\" ");
    console.log("\"" + computerSelection + "\" beats \"" + playerSelection + "\" hence computer won");
    computer++;
    console.log("player point is \"" + player + "\" ");
    console.log("computer points is \"" + computer + "\" ");
  } else if (playerSelection !== "paper" | playerSelection !== "rock" | playerSelection !== "scissor") {
    console.log(playerSelection);
    console.log("  Error:  Enter valid input");
  } else {
    console.log(computerSelection);

    console.log("It's a DRAW")
  }
  return 0;
}

while (player != 5 || computer != 5) {
  var train = ""
  train = prompt("Enter your choice for the game");
  round(train, computerPlay);
  if (computer == 5) {
    console.log(" ********* comupter won ***********");
    break;
  } else if (player == 5) {
    console.log(" ********* player won ***********");
    break;
  }
}

Error:

computer selection is "paper" main.js:30:17
player selection is "rock" main.js:31:17
"paper" beats "rock" hence computer won main.js:32:17
player point is "0" main.js:34:17
computer points is "1" main.js:35:17
computer selection is "rock" main.js:17:21
player selection is "paper" main.js:18:17
"paper" beats "rock" hence player won main.js:19:17
player point is "1" main.js:21:17
computer points is "1" main.js:22:17


**<empty string> main.js:38:17
  Error:  Enter valid input main.js:39:17**
 - Here it is not comparing the string although i have entered "paper".


computer selection is "scissor" main.js:30:17
player selection is "paper" main.js:31:17
"scissor" beats "paper" hence computer won main.js:32:17
player point is "1" main.js:34:17
computer points is "2" 

Upvotes: 1

Views: 45

Answers (1)

Will Metcher
Will Metcher

Reputation: 311

| is a bitwise or, you want || which is logical or.

else if(playerSelection != "paper" || playerSelection != "rock" || playerSelection != "scissor")

Upvotes: 1

Related Questions