我有一个有两个按钮的页面:
每次用户拿起或丢下钻石时,都会有一条消息告诉他总共有多少钻石。
我不希望这些消息过于泛泛,比如:“你有1个菱形(S)”。
我设法生成了不同的消息,这取决于用户拥有的菱形数量,以及他是在拿起它们,还是在丢弃它们:
我也希望有自定义的消息,当用户试图拾起一个钻石时,他的钱包已经满了,当他试图丢弃一个时,他没有任何钻石下降。例如:
“你不能再摘钻石了,你的钱包已经满了。”和“你没有任何钻石可丢弃。”
这就是我需要帮助的地方。
正如您将在下面的代码中看到的,我让diamondCount == 0在函数中放置菱形,以生成消息"You drop your last菱形“。如果用户试图从这一点放入另一个菱形,diamondCount仍将为0。那么,当用户刚刚到达0,以及当他试图从0开始丢弃菱形时,我如何区分它呢?
这就是我所拥有的:
function pickDiamond() {
if (typeof(Storage) !== "undefined") {
if (localStorage.diamondCount < 10) {
localStorage.diamondCount = Number(localStorage.diamondCount) + 1;
} else {
localStorage.diamondCount = 10;
}
if (localStorage.diamondCount == 1) {
document.getElementById("result").innerHTML = "You picked up your first Diamond!";
} else {
document.getElementById("result").innerHTML = "You picked up a Diamond. You now have a total of " + localStorage.diamondCount + " Diamonds in your wallet.";
}
}
}
function dropDiamond() {
if (typeof(Storage) !== "undefined") {
if (localStorage.diamondCount > 0) {
localStorage.diamondCount = Number(localStorage.diamondCount) - 1;
} else {
localStorage.diamondCount = 0;
}
if (localStorage.diamondCount == 1) {
document.getElementById("result").innerHTML = "You dropped 1 Diamond. You only have 1 Diamond left in your wallet.";
} else if (localStorage.diamondCount == 0) {
document.getElementById("result").innerHTML = "You dropped your last Diamond. You don't have any Diamonds now.";
} else {
document.getElementById("result").innerHTML = "You dropped a Diamond. You now have a total of " + localStorage.diamondCount + " Diamonds in your wallet.";
}
}
}
function dropAllDiamonds() {
localStorage.diamondCount = 0;
document.getElementById("result").innerHTML = "You dropped all your Diamonds.";
} <p>Use the buttons to pick up or drop Diamonds</p>
<p><button onclick="pickDiamond()" type="button">Pick up Diamonds</button></p>
<p><button onclick="dropDiamond()" type="button">Drop Diamonds</button></p>
<p><button onclick="dropAllDiamonds()" type="button">Drop All Diamonds</button></p>
<p id="result"></p>
发布于 2018-10-08 04:32:06
看起来你的问题是if/else逻辑的问题之一。具体地说,你应该能够用一个if/else链做任何你想做的事情。我对你的代码做了一些修改,如下所示:
对于pickDiamond,我从钻石计数为零开始,并随着用户库存的增加在逻辑序列中添加新的文本。只处理数字,增量运算符在这里比较合适。一旦用户的库存达到10,逻辑将默认为“您不能再摘钻石了。您的钱包已经满了。”最后的else语句。为了测试,我创建了一个localStorage对象。
let localStorage = {
diamondCount: 0
}
function pickDiamond() {
if (typeof(Storage) !== "undefined") {
if (localStorage.diamondCount == 0) {
localStorage.diamondCount++;
document.getElementById("result").innerHTML = "You picked up your first Diamond!";
} else if (localStorage.diamondCount < 10) {
localStorage.diamondCount++;
document.getElementById("result").innerHTML = "You picked up a Diamond. You now have a total of " + localStorage.diamondCount + " Diamonds in your wallet.";
} else {
document.getElementById("result").innerHTML = "You cannot pick any more diamonds. Your wallet is already full.";
}
}
}类似的解决方案也可以用于dropDiamond函数,但反之亦然。dropAllDiamonds函数不需要改动就能正常工作。
function dropDiamond() {
if (typeof(Storage) !== "undefined") {
if (localStorage.diamondCount > 2) {
localStorage.diamondCount--;
document.getElementById("result").innerHTML = "You dropped a Diamond. You now have a total of " + localStorage.diamondCount + " Diamonds in your wallet.";
} else if (localStorage.diamondCount == 2) {
localStorage.diamondCount--;
document.getElementById("result").innerHTML = "You dropped 1 Diamond. You only have 1 Diamond left in your wallet.";
} else if (localStorage.diamondCount == 1) {
localStorage.diamondCount--;
document.getElementById("result").innerHTML = "You dropped your last Diamond. You don't have any Diamonds now.";
} else {
document.getElementById("result").innerHTML = "You don't have any Diamonds to drop ya dingus";
}
}
}
function dropAllDiamonds() {
localStorage.diamondCount = 0;
document.getElementById("result").innerHTML = "You dropped all your Diamonds.";
}https://stackoverflow.com/questions/52691272
复制相似问题