在数组中查找最低和最高数量值

发布于 2025-02-02 23:40:12 字数 580 浏览 2 评论 0原文

我正在努力锻炼此代码的工作原理,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 技术交流群。

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

发布评论

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

评论(4

遇见了你 2025-02-09 23:40:12

两者在此示例中都从2开始

let minNum = numbers[0]//equals first element in the numbers array and thats 2 in this example
let maxNum = numbers[0]// equals 2 as well

,然后您开始迭代数字阵列,

让我们介绍如何获得最低数字:

if(minNum > numbers[i]){

1st loop
minNum = 2 not greater than 2, nothing happens
2nd loop 
minNum = 2 not greater than 9, nothing happens
3rd loop
minNum = 2 not greater than 10, nothing happens
4th loop
minNum = 2 not greater than 17, nothing happens
last loop
minNum = 2 not greater than 45, nothing happens

minnum =在整个迭代期间永不更改,因为没有找到小于2的数字,因此它可以保留

现在的 初始值Maxnum:

 } else if (maxNum < numbers[i]){

1st loop
maxNum= 2, numbers[i]=2, numbers[i] not greater than maxNum, nothing happens
2nd loop 
maxNum= 2, numbers[i]=9, numbers[i] is greater than maxNum, maxNum=9 now
3rd loop
maxNum= 9, numbers[i]=10, numbers[i] is greater than maxNum, maxNum=10 now
4th loop
maxNum= 10, numbers[i]=17, numbers[i] is greater than maxNum, maxNum=17 now
last loop
maxNum= 17, numbers[i]=45, numbers[i] is greater than maxNum, maxNum=45 now

Maxnum将覆盖自身,只要您找到一个高于先前存储的值的数字即可。
希望这解释了您不了解的

Both start at 2 in this example

let minNum = numbers[0]//equals first element in the numbers array and thats 2 in this example
let maxNum = numbers[0]// equals 2 as well

Then you start to iterate thru the numbers array

Lets cover how you get your lowest number first:

if(minNum > numbers[i]){

1st loop
minNum = 2 not greater than 2, nothing happens
2nd loop 
minNum = 2 not greater than 9, nothing happens
3rd loop
minNum = 2 not greater than 10, nothing happens
4th loop
minNum = 2 not greater than 17, nothing happens
last loop
minNum = 2 not greater than 45, nothing happens

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:

 } else if (maxNum < numbers[i]){

1st loop
maxNum= 2, numbers[i]=2, numbers[i] not greater than maxNum, nothing happens
2nd loop 
maxNum= 2, numbers[i]=9, numbers[i] is greater than maxNum, maxNum=9 now
3rd loop
maxNum= 9, numbers[i]=10, numbers[i] is greater than maxNum, maxNum=10 now
4th loop
maxNum= 10, numbers[i]=17, numbers[i] is greater than maxNum, maxNum=17 now
last loop
maxNum= 17, numbers[i]=45, numbers[i] is greater than maxNum, maxNum=45 now

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

陈年往事 2025-02-09 23:40:12

在上下文中,我没有完全得到我估计您想知道比较的工作方式。

function sortThem(numbers) {

  let minNum = numbers[0]
  let maxNum = numbers[0]

  for (let i = 0; i < numbers.length; i++) { // you can start loop at index 1 since you have set default val to 0
    if (minNum > numbers[i]) { // if my current number in array is smaller than the most recent minNum
      minNum = numbers[i] // set minNum to current number Exit out of if/else structure since else will not be triggered
    } else if (maxNum < numbers[i]) { // if currentNumber is greater than given maxNum
      maxNum = numbers[i] //set maxnum to current number
    }
  }

  console.log(maxNum)

  const minMax = [minNum, maxNum] //useless
  return minMax // return [minNum, maxNum];

}

const results = sortThem([2, 9, 10, 17, 45])

console.log(results)

不知道它是否回答了您的问题,但我认为这是对此功能的解释。

Given to a context I dont fully get I would estimate that you want to know how the comparison works.

function sortThem(numbers) {

  let minNum = numbers[0]
  let maxNum = numbers[0]

  for (let i = 0; i < numbers.length; i++) { // you can start loop at index 1 since you have set default val to 0
    if (minNum > numbers[i]) { // if my current number in array is smaller than the most recent minNum
      minNum = numbers[i] // set minNum to current number Exit out of if/else structure since else will not be triggered
    } else if (maxNum < numbers[i]) { // if currentNumber is greater than given maxNum
      maxNum = numbers[i] //set maxnum to current number
    }
  }

  console.log(maxNum)

  const minMax = [minNum, maxNum] //useless
  return minMax // return [minNum, maxNum];

}

const results = sortThem([2, 9, 10, 17, 45])

console.log(results)

No Idea if it answered your question, but I think thats the explanation of this function.

长亭外,古道边 2025-02-09 23:40:12

您定义minnummaxnum,从数组中的一些值开始(在这种情况下为索引0/第一个,即2)。

for(让i = 0; i&lt; numbers.length; i ++){/* ...*/}
表示{}内部的所有内容都将与数组具有索引相同的次数重复,并且每个迭代i都将增加一个(从0开始,因此它将在所有数字数组的索引上循环)

SO 数字[i]获取i th的值> ...)

如果条件检查当前minnum/maxnumi imumber数组的索引时大/大于值,如果是这样,它将设置minnum/maxnum将其设置为该值,因此增加/减少。

基本上,代码将您定义的数组中的每个值都陷入困境,并且每次找到更大的值时,都会将其设置为minnum/maxnum的值。

在这种情况下,minnum设置为2,并且由于它是最小的,因此保持2。
maxnum从2开始,增加到9、10、17,然后增加到45。

You define minNum and maxNum, 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 at ith position (first time it will be 2, second time 9...)

The if conditions check whether the current minNum/maxNum is smaller/greater than value at ith index of numbers array, and if it is, it sets minNum/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.

江湖正好 2025-02-09 23:40:12

虽然,这是一个简单的实施。如果您使用笔和纸干燥循环会更好。我将写您使用过的算法,以使您变得更容易。

  1. 您将数组作为输入。
  2. 您将2 var(即minnum和maxnum)初始化为第一个值
    数组中的元素
  3. 在数组上运行一个循环,以检查并更新Minnum和
    maxnum。
  4. 循环结束后,您在数组中返回这些数字。
    就是这样!

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.

  1. You take an array as an input.
  2. You initialize 2 vars (namely minNum and maxNum) to the value of the First
    element from the array
  3. Run a loop over the array, to check and update the values of minNum and
    maxNum.
  4. After the loop ends, you return those numbers in an array.
    That's it!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文