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?