是的,我意识到我是那个人"。对不起
问题在于:我参加了Rock Paper Scissors游戏并决定尝试使用jQuery。基本的想法是你应该点击一个按钮为自己做出选择(摇滚,纸张或剪刀),然后当你点击"去"按钮您的选择将与随机生成的计算机选择进行比较。在此之后,将出现一个模态,表明您是赢了,输了还是并列。 我已经通过并在JS中放置了单击侦听器,并且按钮似乎工作正常(他们将控制日志记录他们应该做的事情)。我不确定问题是什么。我猜测我的比较"函数没有被传递正确的变量,因为模态将加载,但它只是说" ERROR!"。
这是HTML
<div class="container">
<div class="row">
<div class="col-sm-4">
<button id="rock">Rock</button>
</div>
<div class="col-sm-4">
<button id="paper">Paper</button>
</div>
<div class="col-sm-4">
<button id="scissors">Scissors</button>
</div>
</div>
<br />
<div class="row">
<div class="col-sm-4 col-sm-offset-4">
<button id="go" data-toggle="modal">Fight!</button>
</div>
</div>
</div>
<div id="bigWin" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">× </button>
<h4 class="modal-title">Results!</h4>
</div>
<div class="modal-body">
<p id="result"></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
这是JS
$( document ).ready(function() {
//computer choice logic
function computerChoice(){
var choices = ['rock', 'paper', 'scissors'];
var computerRando = choices[Math.floor(Math.random()* choices.length)];
return computerRando;
}
//button functions
$("#rock").click(function(){
choose("rock");
});
$("#paper").click(function(){
choose("paper");
});
$("#scissors").click(function(){
choose("scissors");
});
$("#go").click(function(){
compare(choose, computerChoice);
$("#bigWin").modal('show')
});
function choose(userChoice){
if (userChoice === "rock"){
return "rock";
} else if(userChoice === "paper"){
return "paper";
} else if(userChoice === "scissors"){
return "scissors";
}
}
//fire off game
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
document.getElementById("result").innerHTML = "Tie! Train harder!";
}
else if (choice1 === "rock") {
if (choice2 === "scissors") {
document.getElementById("result").innerHTML = "You have vanquished your enemy!";
}
else {
document.getElementById("result").innerHTML = "You have brought shame upon your dojo!!";
}
}
else if (choice1 === "paper") {
if (choice2 === "rock") {
document.getElementById("result").innerHTML = "You have vanquished your enemy!";
}
else if (choice2 ==="scissors") {
document.getElementById("result").innerHTML = "You have brought shame upon your dojo!!";
}
}
else if (choice1 === "scissors") {
if (choice2 === "rock") {
document.getElementById("result").innerHTML = "You have brought shame upon your dojo!!";
}
else if (choice2 === "paper") {
document.getElementById("result").innerHTML = "You have vanquished your enemy!";
}
}
else {
document.getElementById("result").innerHTML = "ERROR!";
}
}
});
感谢任何帮助。
答案 0 :(得分:2)
您调用choose()
但从未将返回值分配给变量。
然后你拨打compare(choose, computerChoice);
,但'choose'
永远不会作为变量存在。
你应该更多地考虑Javascript / JQuery中的变量范围。
您的代码中还有一些错误。但是试着亲自找到它们。