Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
The code I came up with works, but it's not pretty.
String->array->Dict

What would be a better option for less Time/Space Complexity?

I've got hurricane coming in, almost 9am here. Been coding since 4pm yesterday trying to cram before power loss(Hence why I posted 3 questions today.) I am going to sleep now, so I'll have to check after.

Me need sleepy.
Me likey and value your responsey.
Good night/morning/day/time/space.

function hasThreeVowels(str){
    let vowels = "aeiou";
    let newArray = []
    for( let i = 0; i < str.length; i++){
        if(vowels.includes(str[i])){
            newArray.push(str[i]);
        }
        
    }
    let uniqueArray = new Set(newArray);
    if(uniqueArray.size >= 3){
        return true;
    } else {
        return false;
    }
}


What I have tried:

Sleep more, code less, I will.
Posted
Updated 28-Sep-22 2:46am

1 solution

The shortest I was able to come up with was:
JavaScript
function hasThreeVowels(value) {
  const vowels = 'aeiou';
  return [...value].filter(e => vowels.indexOf(e.toLowerCase()) > -1).length >= 3;
}

To break it down:
[...value]
takes the string value and creates an array populated with all the characters.

.filter(e => vowels.indexOf(e.toLowerCase()) > -1)
Then takes the array of characters and only includes those which exist within the vowels string (case-insensitive).

.length >= 3
Then checks whether the resulting array is greater-than-or-equal to three, which is the point of the function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900