当用户尝试减去而没有其他东西可以减去时提醒用户
Alert user when he tries to subtract when there is nothing else more to subtract
我有一个包含两个按钮的页面:
- 将 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>
看来您的问题属于 if/else 逻辑之一。具体来说,你应该能够用一个 if/else 链做你想做的一切。我对您的代码做了一些更改,如下所示:
对于 pickDiamond,我从您的钻石数量为零开始,并随着用户库存的增加在逻辑序列中添加新文本。仅处理数字,增量运算符在这里是合适的。一旦用户的库存达到 10,逻辑将默认为 "You cannot pick any more diamonds. Your wallet is already full." 和最后的 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.";
}
我有一个包含两个按钮的页面:
- 将 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>
看来您的问题属于 if/else 逻辑之一。具体来说,你应该能够用一个 if/else 链做你想做的一切。我对您的代码做了一些更改,如下所示:
对于 pickDiamond,我从您的钻石数量为零开始,并随着用户库存的增加在逻辑序列中添加新文本。仅处理数字,增量运算符在这里是合适的。一旦用户的库存达到 10,逻辑将默认为 "You cannot pick any more diamonds. Your wallet is already full." 和最后的 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.";
}