在数组中查找最低和最高数量值
我正在努力锻炼此代码的工作原理,MaxNum部分如何工作? maxnum [0]<数字[i]如何从数组中获取最后一个数字?
function sortThem(numbers) {
let minNum = numbers[0]
let maxNum = numbers[0]
for (let i = 0; i < numbers.length; i++) {
if (minNum > numbers[i]) {
minNum = numbers[i]
} else if (maxNum < numbers[i]) {
maxNum = numbers[i]
}
}
console.log(maxNum)
const minMax = [minNum, maxNum]
return minMax
}
const results = sortThem([2, 9, 10, 17, 45])
console.log(results)
I am struggling to workout how this code is working, how is the maxNum part working? maxNum[0] < numbers[i] how is it grabbing the last number from the array?
function sortThem(numbers) {
let minNum = numbers[0]
let maxNum = numbers[0]
for (let i = 0; i < numbers.length; i++) {
if (minNum > numbers[i]) {
minNum = numbers[i]
} else if (maxNum < numbers[i]) {
maxNum = numbers[i]
}
}
console.log(maxNum)
const minMax = [minNum, maxNum]
return minMax
}
const results = sortThem([2, 9, 10, 17, 45])
console.log(results)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
两者在此示例中都从2开始
,然后您开始迭代数字阵列,
让我们介绍如何获得最低数字:
minnum =在整个迭代期间永不更改,因为没有找到小于2的数字,因此它可以保留
现在的 初始值Maxnum:
Maxnum将覆盖自身,只要您找到一个高于先前存储的值的数字即可。
希望这解释了您不了解的
Both start at 2 in this example
Then you start to iterate thru the numbers array
Lets cover how you get your lowest number first:
minNum = never changes during the entire iteration becuz no number lesser than 2 was found so it keeps the initial value
Now for the maxNum:
maxNum will overwrite itself as long as you can find a number that's higher than the value previously stored.
Hopefully that explains what you don't understand
在上下文中,我没有完全得到我估计您想知道比较的工作方式。
不知道它是否回答了您的问题,但我认为这是对此功能的解释。
Given to a context I dont fully get I would estimate that you want to know how the comparison works.
No Idea if it answered your question, but I think thats the explanation of this function.
您定义
minnum
和maxnum
,从数组中的一些值开始(在这种情况下为索引0/第一个,即2)。for(让i = 0; i&lt; numbers.length; i ++){/* ...*/}
表示{}内部的所有内容都将与数组具有索引相同的次数重复,并且每个迭代
i
都将增加一个(从0开始,因此它将在所有数字数组的索引上循环)SO
数字[i]
获取i
th的值> ...)如果条件检查当前
minnum
/maxnum
在i
imumber数组的索引时大/大于值,如果是这样,它将设置minnum
/maxnum
将其设置为该值,因此增加/减少。基本上,代码将您定义的数组中的每个值都陷入困境,并且每次找到更大的值时,都会将其设置为
minnum
/maxnum
的值。在这种情况下,
minnum
设置为2,并且由于它是最小的,因此保持2。maxnum
从2开始,增加到9、10、17,然后增加到45。You define
minNum
andmaxNum
, which start with being just some value from the array (in this case index 0 / the first one, which is 2).for (let i = 0; i < numbers.length; i++) { /* ...*/ }
means everything inside {} will be repeated the same amount of times that the array has indexes, and every iteration
i
will be increased by one (starting with 0, so it will loop over all indexes of numbers array)so
numbers[i]
gets value ati
th position (first time it will be2
, second time9
...)The if conditions check whether the current
minNum
/maxNum
is smaller/greater than value ati
th index of numbers array, and if it is, it setsminNum
/maxNum
to that value, so it is increased / decreased.Basically the code goes trough every value in the array you defined, and everytime it finds bigger value, it sets it as value for
minNum
/maxNum
.In this case
minNum
is set to 2, and because it is the smallest of all, it stays 2.maxNum
starts at 2, is increased to 9, 10, 17 and then to 45.虽然,这是一个简单的实施。如果您使用笔和纸干燥循环会更好。我将写您使用过的算法,以使您变得更容易。
数组中的元素
maxnum。
就是这样!
Although, its an easy implementation. It'll be better if you dry run the loop using a pen and paper. I'll write the algo you have used so that it becomes easier for you.
element from the array
maxNum.
That's it!