Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

146
Visualizações
Finding lowest and highest number number value in an Array

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)

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda