为什么 JavaScript 的行为不同

发布于 2025-01-10 15:31:17 字数 1030 浏览 0 评论 0原文

为什么它显示我 36,即使最小数字是 27

   var combination = [27, 36]
    
    for (let x in combination) {
        if (combination[x] < 50) {
            var min = Math.min(combination[x])
        }
    }
    
    console.log(min)

我尝试了多种方法,就像

    var combination = [27, 30, 40, 44, 3, 239, 329, 2, 5, 20923, 96]
    
    for (let x in combination) {
        if (combination[x] < 50) {
            var min = Math.min(combination[x])
        }
    }
    
    console.log(min) //output--  5         //it should be 2

在第三个示例中

var combination = [27, 30, 40, 44, 3, 239, 329, -2, 5, 20923, 96]

for (let x in combination) {
    if (combination[x] < 50) {
        var min = Math.min(combination[x])
    }
}

console.log(min) // output-- still 5     // it should be -2 

一样,当我将 (-) 添加到其他数字(例如 -96 或 -5)时,我再次将 (-) 添加到 2 输出是好的(-96)但是当我将(-)添加到2时,它并没有在输出中显示-2,而是

不仅在javascript中显示5,我用lua,php尝试了这个,但输出与js相同,

任何人都可以解释我为什么会发生这种情况以及如何解决这个问题

why it is showing me 36 even though the minimum number is 27

   var combination = [27, 36]
    
    for (let x in combination) {
        if (combination[x] < 50) {
            var min = Math.min(combination[x])
        }
    }
    
    console.log(min)

i tried this multiple ways like

    var combination = [27, 30, 40, 44, 3, 239, 329, 2, 5, 20923, 96]
    
    for (let x in combination) {
        if (combination[x] < 50) {
            var min = Math.min(combination[x])
        }
    }
    
    console.log(min) //output--  5         //it should be 2

in this third example i add (-) to 2

var combination = [27, 30, 40, 44, 3, 239, 329, -2, 5, 20923, 96]

for (let x in combination) {
    if (combination[x] < 50) {
        var min = Math.min(combination[x])
    }
}

console.log(min) // output-- still 5     // it should be -2 

again when am adding (-) to other numbers like in -96 or -5 the output was okay (-96) but when im adding (-) to 2 it is not showing me -2 in the output instead it showing me 5

not only in javascript i tried this with lua, php but output was same as js

can anyone explain me why this happen and how solve this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2025-01-17 15:31:17

您不是通过比较值来确定最小值,而是只是将 min 变量替换为数组中小于 50 的最后一个数字。这可以通过如下方式修复:

let min = undefined;
for (let x in combination) {
    if (combination[x] < 50) {
        min = min == undefined ? combination[x] : Math.min(min, combination[x])
    }
}

使用 filterreduce,可以使其变得更短:

combination.filter(x => x < 50).reduce((x, y) => Math.min(x, y))

You're not comparing values to determine the minimum, but instead just replacing the min variable with the last number in the array that is smaller than 50. This can be fixed as follows:

let min = undefined;
for (let x in combination) {
    if (combination[x] < 50) {
        min = min == undefined ? combination[x] : Math.min(min, combination[x])
    }
}

Using filter and reduce, this can be made a lot shorter:

combination.filter(x => x < 50).reduce((x, y) => Math.min(x, y))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文