我有一个带有两个按钮的页面: 一个会向用户的清单添加1单位的物品。 另一个从用户的清单中删除1单位的东西。 在此示例中,某些东西将是钻石。
每次用户拿起或丢弃钻石时,都会有一条消息告诉他总共有多少钻石。
我不希望这些消息过于笼统,例如:“您有1颗钻石”。
我设法根据用户拥有的钻石数量以及他是捡起还是扔掉钻石来生成不同的消息:
我还希望收到自定义消息,以供用户在钱包已满的情况下尝试捡起钻石,以及在他没有要放任何钻石的情况下尝试放下钻石的消息。例如:
“您不能再采摘钻石。您的钱包已满。”和“您没有可放的钻石。”
这就是我需要的帮助。
您将在下面的代码中看到,我的函数中有diamondCount == 0
,该函数会丢弃钻石以生成消息“您丢弃了最后一颗钻石”。如果用户尝试从此时开始放下另一个菱形,则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>
答案 0 :(得分:1)
您的问题似乎是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.";
}