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

90
Visualizações
Elements are getting omitted from Javascript array - problem

I want to sort an array from lowest number to highest. But when I execute the following code, some of my array elements get missed! I don't know why. I need help.`// Sort an array from lowest to highest

function findSmallest(numArr) {
  let smallestNumber = numArr[0]

  for (let i = 0; i < numArr.length; i++) {
    if (numArr[i + 1] < smallestNumber) {
      smallestNumber = numArr[i + 1]
    }
  }
  return smallestNumber;
}

function getSortedArray(arr) {
  let sortedArray = []


  for (j = 0; j < arr.length; j++) {
    let smallest = findSmallest(arr)
    let smallestNumbmerIndex;

    for (let i = 0; i < arr.length; i++) {
      if (arr[i] === smallest) {
        smallestNumbmerIndex = i
      }
    }
    arr.splice(smallestNumbmerIndex, 1)
    sortedArray.push(smallest)
  }

  return sortedArray
}

let myArr = [23, 2, 12, 4]
console.log(getSortedArray(myArr)) // console output => [2, 4] , the rest elements get omitted

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

0

since arr shrinks in each iteration, your for loop won't run 4 iterations, only 2 -

use while(arr.length){ instead of the for j loop

function findSmallest(numArr) {
  let smallestNumber = numArr[0]

  for (let i = 0; i < numArr.length; i++) {
    if (numArr[i + 1] < smallestNumber) {
      smallestNumber = numArr[i + 1]
    }
  }
  return smallestNumber;
}

function getSortedArray(arr) {
  let sortedArray = []


  while(arr.length) {
    let smallest = findSmallest(arr)
    let smallestNumbmerIndex;

    for (let i = 0; i < arr.length; i++) {
      if (arr[i] === smallest) {
        smallestNumbmerIndex = i
      }
    }
    arr.splice(smallestNumbmerIndex, 1)
    sortedArray.push(smallest)
  }

  return sortedArray
}

let myArr = [23, 2, 12, 4]
console.log(getSortedArray(myArr)) // console output => [2, 4] , the rest elements get omitted

about 4 years ago · Juan Pablo Isaza Relatório

0

We can use a while loop, or we can store the length of array into a variable before the for loop, so that it will not change:

function findSmallest(numArr) {
  let smallestNumber = numArr[0]

  for (let i = 0; i < numArr.length; i++) {
    if (numArr[i + 1] < smallestNumber) {
      smallestNumber = numArr[i + 1]
    }
  }
  return smallestNumber;
}

function getSortedArray(arr) {
  let sortedArray = []

  // we can store the array length into a variable
  let length = arr.length
  for (j = 0; j < length; j++) {
    let smallest = findSmallest(arr)
    let smallestNumbmerIndex;

    for (let i = 0; i < arr.length; i++) {
      if (arr[i] === smallest) {
        smallestNumbmerIndex = i
      }
    }
    arr.splice(smallestNumbmerIndex, 1)
    sortedArray.push(smallest)
  }

  return sortedArray
}

let myArr = [23, 2, 12, 4]
console.log(getSortedArray(myArr)) 

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