= eval(secondvalue)) { co-6ren">
gpt4 book ai didi

javascript - 如何在比较中替换两个 eval()

转载 作者:行者123 更新时间:2023-12-02 17:10:16 24 4
gpt4 key购买 nike

在以下代码片段中:

    case "lessthan": {
if (eval(firstvalue) >= eval(secondvalue)) {
compare = " should be less than ";
compReturn = false;
}
break;

我想删除 eval() 部分以获得更高效的代码,其中内容预计为数字。我读过其他类似的问题,但我不明白,所以我希望这是一个简单的案例!我从

开始
     if parseFloat(firstvalue) >= parseFloat(secondvalue) {

但是读了这么多关于 eval() 的内容后,我感觉我错过了一些东西。

最佳答案

如果您需要 int 值,则可以使用 parseInt,并且还应该检查返回值以确保返回可用的数字。

case "lessthan": {
var a = parseInt(firstvalue, 10), // 10 specifies base, so inputs aren't
b = parseInt(secondvalue, 10); // interpreted as hex, octal, etc
if ( isNaN(a) || isNaN(b) ) {
// TODO a parse failed, handle your error condition
}
else if (a >= b) {
compare = " should be less than ";
compReturn = false;
}
break;

关于javascript - 如何在比较中替换两个 eval(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24876942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com