gpt4 book ai didi

javascript - 如何求数组中除0之外的最小值?

转载 作者:行者123 更新时间:2023-12-02 18:09:30 25 4
gpt4 key购买 nike

我有一个包含一些值的数组,我想找到该数组的最小值,以便打印出该值所具有的索引。在这个数组中,其中一个值是 0。我的猜测是,为了找到索引,我们必须迭代这个数组,找到最小值,但不是 0,然后返回索引。你能帮我理解我在迭代中做错了什么吗?我找不到最小数量。

这是数组:

[ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75 ]

我目前被困在这里:

  var smallest = 0
var biggest = 0

for (let i = 0; i < merged.length; i++) {


if (merged[i] > biggest && merged[i] != 0) {
biggest = merged[i];

} else merged[i] < smallest ? smallest = merged[i] : smallest = merged[i];



console.log('the biggest is', biggest, 'in the iteration', i)
console.log('the smallest is', smallest, 'in the iteration', i)
}


console.log('min-> : ', smallest, biggest);

这给出了:

the biggest is 10 in the iteration 0
the smallest is 0 in the iteration 0
the biggest is 10 in the iteration 1
**the smallest is 5 in the iteration 1**
the biggest is 10 in the iteration 2
**the smallest is 6 in the iteration 2**
the biggest is 10 in the iteration 3
the smallest is 5.5 in the iteration 3
the biggest is 10 in the iteration 4
the smallest is 3.75 in the iteration 4
the biggest is 10 in the iteration 5
the smallest is 0 in the iteration 5
the biggest is 10 in the iteration 6
the smallest is 4.25 in the iteration 6
the biggest is 10 in the iteration 7
the smallest is 3 in the iteration 7
the biggest is 10 in the iteration 8
the smallest is 5.5 in the iteration 8
the biggest is 10 in the iteration 9
the smallest is 6.75 in the iteration 9
the biggest is 10 in the iteration 10
the smallest is 8 in the iteration 10
the biggest is 10 in the iteration 11
the smallest is 9.25 in the iteration 11
the biggest is 10 in the iteration 12
the smallest is 4 in the iteration 12
the biggest is 15 in the iteration 13
the smallest is 4 in the iteration 13
the biggest is 15 in the iteration 14
the smallest is 4.25 in the iteration 14
the biggest is 15 in the iteration 15
the smallest is 6 in the iteration 15
the biggest is 15 in the iteration 16
the smallest is 6 in the iteration 16
the biggest is 15 in the iteration 17
the smallest is 4.75 in the iteration 17
the biggest is 15 in the iteration 18
the smallest is 3.75 in the iteration 18
min-> : 3.75 15

正如您在上面看到的,最小的值不应从 5 更改为 6。最小值应为 3

非常感谢。

问题已解决,感谢大家的时间和帮助!!

最佳答案

只需将代码更改为以下内容即可,

var smallest = 0;
var biggest = 0;
var merged = [ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75 ];

for (let i = 0; i < merged.length; i++) {

if (merged[i] > biggest && merged[i] != 0) {
biggest = merged[i];
}

if(smallest == 0 || (merged[i] < smallest && merged[i] != 0)) {
smallest = merged[i];
}

console.log('the biggest is', biggest, 'in the iteration', i)
console.log('the smallest is', smallest, 'in the iteration', i)
}

我假设您不需要检查数组中 0 值项的索引。

关于javascript - 如何求数组中除0之外的最小值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72635382/

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