JavaScript提供错误的输出

时间:2019-04-17 15:00:55

标签: javascript

我正在创建游戏(纸/剪刀/石头),在最后一节中,我想比较userChoice与computerChoice,但是由于某种原因,我得到了错误的输出:当我按下按钮时,例如“ Stone”和计算机选择“剪刀”,我得到以下输出:“你选择石头。电脑选择剪刀你输了!再试一次。'但这是错误的!应该在另一边。 当“这是一条领带!”时,我也没有得到回报。 你能帮忙吗?

//user choice
var output = document.getElementById("output");
var result = document.getElementById("result");
var count = 3;
var countUser = 0;
var countComputer = 0;
var paper = document.querySelector("#paper header");
paper.addEventListener("click", function() {
  paper.classList.toggle("header-special");
  userChoice = "paper";
  output.innerHTML = "You  Chose Paper";
  compareWithComputer("paper");
});

var scissors = document.querySelector("#scissors header");
scissors.addEventListener("click", function() {
  scissors.classList.toggle("header-special");
  userChoice = "scissors";
  output.innerHTML = "You  Chose Scissors";
  compareWithComputer("scissors");
});

var stone = document.querySelector("#stone header");
stone.addEventListener("click", function() {
  stone.classList.toggle("header-special");
  userChoice = "stone";
  output.innerHTML = "You  Chose Stone";
  compareWithComputer("stone");
});

// Computer choice

function compareWithComputer(userChoice) {
  var computerChoice = Math.floor(Math.random() * 3 + 1);
  if (computerChoice == 1) {
    computerChoice = "Stone";
  } else if (computerChoice == 2) {
    computerChoice = "Paper";
  } else {
    computerChoice = "Scissors";
  }

  var results = compare(userChoice, computerChoice);

  output.innerHTML +=
    ". Computer Chose " +
    computerChoice +
    results;
  result.innerHTML = "user " + countUser + "computer" + countComputer;
}
// Compare userChoice and computerChoice
var compare = function(choice1, choice2) {
  if (choice1 === choice2) {
    return "It's a tie!";
  }
  if (choice1 === "stone") {
    if (choice2 === "scissors") {
      // stone wins
      countUser++;
      return "You win!";

    } else {
      // paper wins
      countComputer++;
      return "You lose! Try again.";

    }
  }
  if (choice1 === "paper") {
    if (choice2 === "stone") {
      // paper wins
      countUser++;
      return "You win!";


    } else {
      // scissors wins
      countComputer++;
      return "You lose! Try again.";

    }
  }
  if (choice1 === "scissors") {
    if (choice2 === "stone") {
      // stone wins
      countComputer++;
      return "You lose! Try again.";

    } else {
      // scissors wins
      countUser++;
      return "You win!";

    }
  }
};
<!DOCTYPE html>
<div class="start" <h1>Click the button, start the game!</h1>
</div>
<div class="game" id="paper">
  <header>Paper</header>

</div>
<div class="game" id="scissors">
  <header>Scissors</header>
</div>
<div class="game" id="stone">
  <header>Stone</header>
</div>
<div id="output"></div>
<div id="result" </div>

1 个答案:

答案 0 :(得分:1)

在函数SELECT base, MAX(adjustment) AS max_adjustment, base + IIF(MAX(adjustment) > 0, 'A', '') + MAX(adjustment) max_adjusted_claim FROM ( SELECT IIF(PATINDEX('%A%', ClaimID) = 0, ClaimID, LEFT(ClaimID, PATINDEX('%A%', ClaimID)-1)) AS base, REPLACE(REVERSE(LEFT(REVERSE(ClaimID), PATINDEX('%A%', REVERSE(ClaimID)))), 'A', '') AS adjustment FROM table_name )t GROUP BY base 中,您返回以大写字母开头的字符串。这些不等于其小写字母(compareWithComputer)的对应字符。

此代码有效:

'Stone' !== 'stone'
    //user choice
    var output =document.getElementById("output");
    var result =document.getElementById("result");
    var count=3;
    var countUser=0;
    var countComputer=0;
    var paper = document.querySelector("#paper header");
    paper.addEventListener("click", function() {
    paper.classList.toggle("header-special");
    userChoice = "paper";
    output.innerHTML = "You  Chose Paper";
    compareWithComputer("paper");
    });

    var scissors = document.querySelector("#scissors header");
    scissors.addEventListener("click", function() {
    scissors.classList.toggle("header-special");
    userChoice = "scissors";
    output.innerHTML = "You  Chose Scissors";
    compareWithComputer("scissors");
    });

    var stone = document.querySelector("#stone header");
    stone.addEventListener("click", function() {
    stone.classList.toggle("header-special");
    userChoice = "stone";
    output.innerHTML = "You  Chose Stone";
    compareWithComputer("stone");
    });

    // Computer choice

    function compareWithComputer(userChoice) {
    var computerChoice = Math.floor(Math.random() * 3 + 1);
    if (computerChoice == 1) {
    computerChoice = "stone";
    } else if (computerChoice == 2) {
    computerChoice = "paper";
    } else {
    computerChoice = "scissors";
    }
 
    var results = compare(userChoice, computerChoice);
  
    output.innerHTML +=
      ". Computer Chose " +
      computerChoice +
      results;
      result.innerHTML="user "+countUser+"computer"+countComputer;
      }
     // Compare userChoice and computerChoice
     var compare = function(choice1, choice2) {
      if (choice1 === choice2) {
     return "It's a tie!";
    }
    if (choice1 === "stone") {
    if (choice2 === "scissors") {
      // stone wins
      countUser++;
      return "You win!";
      
    } else {
      // paper wins
      countComputer++;
      return "You lose! Try again.";
      
    }
    }
    if (choice1 === "paper") {
    if (choice2 === "stone") {
      // paper wins
      countUser++;
      return "You win!";
      
      
    } else {
      // scissors wins
      countComputer++;
      return "You lose! Try again.";
      
    }
    }
    if (choice1 === "scissors") {
    if (choice2 === "stone") {
      // stone wins
      countComputer++;
      return "You lose! Try again.";
      
    } else {
      // scissors wins
      countUser++;
      return "You win!";
      
    }
  }
};