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?
CodePudding user response:
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);
CodePudding user response:
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' ]))