【问题标题】:JavaScript giving wrong outputJavaScript 给出错误的输出
【发布时间】:2019-04-17 15:00:55
【问题描述】:

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

//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>

【问题讨论】:

  • 请将您的代码作为可运行的 sn-p 提供。
  • 在函数“compareWithComputer”中返回以大写开头的字符串。这些不等于小写字母('Stone' !== 'stone')。

标签: javascript


【解决方案1】:

在函数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!";
      
    }
  }
};
    <!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>

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 2020-09-08
    • 2021-02-11
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多