>= 和 <= 似乎不适用于双数字? Javascript
The >= and <= does not seem to work well with double digital? Javascript
发生的事情是我注意到我正在使用 if 语句并检查我是否有足够的分数来获得 >= 的东西但是 运行 遇到一个问题,它可以使用三位数,例如100 或 200,但一旦我尝试做 50,它似乎无法检查它,导致即使您有大量积分也无法购买。我确实注意到当我将最大点数增加到 500 或更高时它会起作用,但我不想那样做。我最近发现的另一个解决方案是这样的 050,但是说 50 很尴尬,你们知道为什么支票很难用两位数或正确的方法来做到这一点吗?
conflict/error是在上次“测试”之前在Sword中创建的,也可以只用评论找到它,这是一个长屁句。
function equipment() {
clear();
console.log(point);
$('<p class=\'texts\'> Here is your equipment screen, please select what you want and if make a mistake or dislike your choice, please click on reset button, otherwise here is your ' + point + ' points to spent on here.</p>').insertBefore('#placeholder');
$('<button class=\'command_button e-buttons\' value=\'pistol\'>Pistol</button>').insertBefore('#placeholder_choice');
$('<button class=\'command_button e-buttons\' value=\'rifle\'>Rifle</button>').insertBefore('#placeholder_choice');
$('<button class=\'command_button e-buttons\' value=\'sword\'>Sword</button>').insertBefore('#placeholder_choice');
$('<button class=\'command_button e-buttons\' value=\'test\'>Test</button>').insertBefore('#placeholder_choice');
};
$(document).on('click', '.e-buttons', function() {
var button = $(this).val();
console.log('The Button is ' + button);
$('#console').scrollTop($('#console')[0].scrollHeight);
if (point <= 0) {
alert("Sorry, you have no more point left, please either continue to next step or remove other choices.");
equipment();
}
if (button == 'pistol') {
if (point >= '100') {
console.log("Your equipment menu is working. " + button);
point = point - '100';
console.log("the point remaining is " + point + ".");
equipment();
}
else {
alert("Sorry not enough money for pistol.");
equipment();
}
}
else if (button == 'rifle') {
if (point >= '200') {
console.log("Your equipment menu is working. " + button)
point = point - '200';
console.log("the point remaining is " + point + ".")
equipment();
}
else {
alert("Sorry not enough money for rifle.");
equipment();
}
}
else if (button == 'sword') {
if (point >= '50') {
console.log("Your equipment menu is working. " + button)
point = point - '50';
console.log("the point remaining is " + point + ".")
equipment();
}
else { // Fix this, something is causing it to be error when it at 50, but if it 100 or more, it work fine. Also notice that if you reduced total to below 400, it will cause same error but are fine if over 400... So something is clearly prevent point check of sword from see the point. Maybe if change to switch statement will correct this?
alert("Sorry not enough money for sword.");
equipment();
}
}
else if (button == 'test') {
if (point >= '500') {
console.log("Your equipment menu is working. " + button)
point = point - '500';
console.log("the point remaining is " + point + ".")
equipment();
}
else {
alert("Sorry not enough money for test.");
equipment();
}
}
else {
console.log("Your equipment menu is not working. " + button)
alert("Your equipment menu is not working. " + button)
};
});```
在您的代码中,您在任何地方都将这些点用作字符串值,但您却像“-”一样对其执行操作。
if (point >= '500') {
或
point = point - '500';
这是故意的吗?
也许将点数更改为整数会有所帮助。
point = point - 500;
问题是您正在比较字符串。例如,当你比较 '200'
和 '50'
时,它实际上会从左到右逐个字符地进行比较。所以我们有来自 2 个字符串的前 2 个字符:'2' < '5'
这将导致 '200' >= '50'
返回 false
.
要解决这个问题,只需在比较之前将它们转换为数字即可。 parseInt()
或 Number()
都可以。
发生的事情是我注意到我正在使用 if 语句并检查我是否有足够的分数来获得 >= 的东西但是 运行 遇到一个问题,它可以使用三位数,例如100 或 200,但一旦我尝试做 50,它似乎无法检查它,导致即使您有大量积分也无法购买。我确实注意到当我将最大点数增加到 500 或更高时它会起作用,但我不想那样做。我最近发现的另一个解决方案是这样的 050,但是说 50 很尴尬,你们知道为什么支票很难用两位数或正确的方法来做到这一点吗?
conflict/error是在上次“测试”之前在Sword中创建的,也可以只用评论找到它,这是一个长屁句。
function equipment() {
clear();
console.log(point);
$('<p class=\'texts\'> Here is your equipment screen, please select what you want and if make a mistake or dislike your choice, please click on reset button, otherwise here is your ' + point + ' points to spent on here.</p>').insertBefore('#placeholder');
$('<button class=\'command_button e-buttons\' value=\'pistol\'>Pistol</button>').insertBefore('#placeholder_choice');
$('<button class=\'command_button e-buttons\' value=\'rifle\'>Rifle</button>').insertBefore('#placeholder_choice');
$('<button class=\'command_button e-buttons\' value=\'sword\'>Sword</button>').insertBefore('#placeholder_choice');
$('<button class=\'command_button e-buttons\' value=\'test\'>Test</button>').insertBefore('#placeholder_choice');
};
$(document).on('click', '.e-buttons', function() {
var button = $(this).val();
console.log('The Button is ' + button);
$('#console').scrollTop($('#console')[0].scrollHeight);
if (point <= 0) {
alert("Sorry, you have no more point left, please either continue to next step or remove other choices.");
equipment();
}
if (button == 'pistol') {
if (point >= '100') {
console.log("Your equipment menu is working. " + button);
point = point - '100';
console.log("the point remaining is " + point + ".");
equipment();
}
else {
alert("Sorry not enough money for pistol.");
equipment();
}
}
else if (button == 'rifle') {
if (point >= '200') {
console.log("Your equipment menu is working. " + button)
point = point - '200';
console.log("the point remaining is " + point + ".")
equipment();
}
else {
alert("Sorry not enough money for rifle.");
equipment();
}
}
else if (button == 'sword') {
if (point >= '50') {
console.log("Your equipment menu is working. " + button)
point = point - '50';
console.log("the point remaining is " + point + ".")
equipment();
}
else { // Fix this, something is causing it to be error when it at 50, but if it 100 or more, it work fine. Also notice that if you reduced total to below 400, it will cause same error but are fine if over 400... So something is clearly prevent point check of sword from see the point. Maybe if change to switch statement will correct this?
alert("Sorry not enough money for sword.");
equipment();
}
}
else if (button == 'test') {
if (point >= '500') {
console.log("Your equipment menu is working. " + button)
point = point - '500';
console.log("the point remaining is " + point + ".")
equipment();
}
else {
alert("Sorry not enough money for test.");
equipment();
}
}
else {
console.log("Your equipment menu is not working. " + button)
alert("Your equipment menu is not working. " + button)
};
});```
在您的代码中,您在任何地方都将这些点用作字符串值,但您却像“-”一样对其执行操作。
if (point >= '500') {
或
point = point - '500';
这是故意的吗? 也许将点数更改为整数会有所帮助。
point = point - 500;
问题是您正在比较字符串。例如,当你比较 '200'
和 '50'
时,它实际上会从左到右逐个字符地进行比较。所以我们有来自 2 个字符串的前 2 个字符:'2' < '5'
这将导致 '200' >= '50'
返回 false
.
要解决这个问题,只需在比较之前将它们转换为数字即可。 parseInt()
或 Number()
都可以。