该代码中的说明应:
比较targetNumber与totalScore
$(document).ready(function() {
var totalScore = 0;
var targetNumber = Math.floor(Math.random() * 120) + 12;
$("#targetNumber").html(targetNumber);
var ruby = Math.floor(Math.random() * 10) + 1;
console.log(ruby);
var diamond = Math.floor(Math.random() * 10) + 1;
console.log(diamond);
var emerald = Math.floor(Math.random() * 10) + 1;
console.log(emerald);
var bloodstone = Math.floor(Math.random() * 10) + 1;
console.log(bloodstone);
$("#ruby").click(function() {
totalScore = totalScore + ruby;
console.log("totalScore");
$("#totalScore").html(totalScore + ruby);
});
$("#diamond").click(function() {
totalScore = totalScore + diamond;
console.log("totalScore");
$("#totalScore").html(totalScore + diamond);
});
$("#emerald").click(function() {
totalScore = totalScore + emerald;
console.log("totalScore");
$("#totalScore").html(totalScore + emerald);
});
$("#bloodstone").click(function() {
totalScore = totalScore + bloodstone;
console.log("totalScore");
$("#totalScore").html(totalScore + bloodstone);
});
});
$(document).ready(function() {
var totalScore = 0;
console.log(totalScore);
$("#totalScore").html(totalScore);
var ruby = Math.floor(Math.random() * 10) + 1;
console.log(ruby);
var diamond = Math.floor(Math.random() * 10) + 1;
console.log(diamond);
var emerald = Math.floor(Math.random() * 10) + 1;
console.log(emerald);
var bloodstone = Math.floor(Math.random() * 10) + 1;
console.log(bloodstone);
var wins = 0;
$("#wins").html(wins)
var losses = 0;
$("#losses").html(losses)
$("#ruby").click(function() {
totalScore = totalScore + ruby;
console.log("totalScore");
$("#totalScore").html(totalScore + ruby);
});
$("#diamond").click(function() {
totalScore = totalScore + diamond;
console.log("totalScore");
$("#totalScore").html(totalScore + diamond);
});
$("#emerald").click(function() {
totalScore = totalScore + emerald;
console.log("totalScore");
$("#totalScore").html(totalScore + emerald);
});
$("#bloodstone").click(function() {
totalScore = totalScore + bloodstone;
console.log("totalScore");
$("#totalScore").html(totalScore + bloodstone);
});
if (totalScore < targetNumber); {
$("#totalScore").html(totalScore);
return(totalScore);
}
if (totalScore > targetNumber); {
$("#totalScore").html(totalScore);
alert("you have exceeded the target number - Try again.");
losses++;
$("#losses").html(losses);
start();
}
if (totalScore = targetNumber); {
$("#totalScore").html(totalScore);
wins++;
$("#wins").html(wins);
alert("Nailed it!");
}})
body {
background: #D2B48C;
}
ul {
margin: 0 auto;
margin-top:50px;
padding: 0;
width: 320px;
}
ul li {
list-style: none;
display: inline-block;
}
<!--
javascript
Crystals: Ruby, Diamond, Emerald, Blodstone
value between 1-10
each click adds crystal value to totalScore
targetNumber
randomly generated
totalScore
sum of crystal clicks
WinsLosses
If totalScore = targetNumber + 1 Win and Reset
If totalScore > targetNumber + 1 Loss and Reset
-->
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="assets/css/style.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Crystal Game</title>
</head>
<body>
<textarea id = "targetNumber" rows="4" cols="50">
"Hit this number without going over"
</textarea> (targetNumber)
<textarea id = "totalScore" rows="4" cols="50">
"Your total score equals"
</textarea> (totalScore)
<textarea id = "wins" rows="4" cols="50">
"wins"
</textarea> (wins)
<textarea id = "losses" rows="4" cols="50">
"losses"
</textarea> (losses)
<button type="button" id = "ruby" >Ruby</button>
<button type="button" id = "diamond" >Diamond</button>
<button type="button" id = "emerald" >Emerald</button>
<button type="button" id = "bloodstone" >Bloodstone</button>
<script src="http://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
<script src="assets/javaScript/crystal.js"></script>
</body>
</html>
如果totalScore超过targetNumber THEN点+ 1损失并重置游戏
如果totalScore小于targetNumber,则继续游戏
如果totalScore等于targetNumber THEN +1获胜并重置游戏
由于某种原因,比较和功能未执行。
我不认为这是语法错误,而且我已经看了数十遍代码,也看不出为什么它不起作用。我在这里想念什么?
最佳答案
比较应该像这样totalScore === targetNumber
而不是totalScore = targetNumber