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

67
Visualizações
Issue with nested for loops in Javascript - Basic Algorithm Scripting

I am trying to write a function that accepts an array of strings as an argument. I want to return the array of strings sorted first by number of vowels and then alphabetically.

Input: ['dog', 'cat', 'elephant', 'hippo', 'goat'] Output: ['cat', 'dog', 'goat', 'hippo', 'elephant']

function vowelMatching(vowels) {
  return vowels.match(/[aeiou]/ig) || []; 
  //The 'i' in regex accounts for capitals 
}

function arrangeByVowels(array) {

  const outputArray = []; 
  console.log(array.length)
  console.log(outputArray.length)
  
   for(let x=0; array.length === outputArray.length; x++) { 
  // let x = 0
    
   const filterArray = [];

    for(let i =0; i<array.length; i++) {
      
       //console.log(vowelMatching(array[i]).length)
       if (vowelMatching(array[i]).length === x) {
       filterArray.push(array[i])
       }  
    }
  
  //console.log(filterArray.sort())
  outputArray.push(...filterArray.sort())
}
  return outputArray
}

console.log(arrangeByVowels(['dog', 'gAg', 'qq', 'cat', 'elephant', 'hippo', 'goat']))

I am using a nested for loop to achieve this. If I assign a value to x for example let x = 1 and I comment out the outer for loop, it will return all the words with 1 vowel in alphabetical order. (This is the result I am after)

I want to loop through x until array.length === outputArray.length but at the moment this code is returning an empty array, and I am not sure why.

I think I am close, but have no idea where I am going wrong. What am I doing wrong?

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

0

As a follow up answer to your actual question....

The issue is the logic in your initial for loop

for(let x=0; array.length === outputArray.length; x++)

Your condition array.length === outputArray.length will always initially return false as your output array is a different length to your initial array. This will instantly end the for loop because the condition to continue the iteration has failed.

The for loop is essentially while(contition is true) increment x; Your condition starts false.

to correct it simple change the condition so it is always true until your criteria is met:

for(let x=0; outputArray.length < array.length; x++)

function vowelMatching(vowels) {
  return vowels.match(/[aeiou]/ig) || []; 
}

function arrangeByVowels(array) {
  const outputArray = []; 
  for(let x=0; outputArray.length < array.length; x++) { // Logic error was here.
    const filterArray = [];
    for(let i =0; i<array.length; i++) {
      if (vowelMatching(array[i]).length === x) {
        filterArray.push(array[i])
      }  
    }
    outputArray.push(...filterArray.sort())
  }
  return outputArray
}

console.log(arrangeByVowels(['dog', 'gAg', 'porcupine', 'qq', 'alligator', 'cat', 'elephant', 'albatross', 'hippo', 'goat', 'potato', 'banana','aardvark' ]))

about 4 years ago · Juan Pablo Isaza Relatório

0

This can be done in a single .sort();

let source = ['dog', 'gAg', 'qq', 'cat', 'elephant', 'hippo', 'goat', 'arose', 'unity', 'arise']

let result = source.sort((a,b) => {
  let aVowels = a.match(/[aeiou]/gi) || [];
  let bVowels = b.match(/[aeiou]/gi) || [];
  return aVowels.length === bVowels.length ? 
     (a > b ? 1 : -1) : // same number of vowels so compare alphabetically
     (aVowels.length > bVowels.length ? 1 : -1) // different number of vowels so compare lengths
})

console.log(result);

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