Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I loop through an Array while referencing the Index to see if the array has multiple of the same vowel?


Why on Earth does it return true for ['d','o','g'] and the wrong answer for just aboout everything else?



function hasThreeVowels(str){
    vowels = "aeiou";
    newArray = []
    for( let i = 0; i < str.length; i++){
        if(vowels.includes(str[i])){
            newArray.push(str[i]);
        }
        
    }
    for( let i = 0; i < newArray.length; i++){
        if(newArray[i] === newArray[i+1]){
            return false;
        }
    }
    return true;
}


What I have tried:

    for( let i = 0; i < newArray.length; i++){
        if(newArray[i] === newArray[i+1]){
            return false;
        }
    }
    return true;
}
Posted
Updated 28-Sep-22 1:22am

1 solution

Look at your code. If any two letters are the same vowel, i.e. are equal, then you return false. For anything else you return true. And since there is only one vowel in 'dog', the test:
JavaScript
if(newArray[i] === newArray[i+1]){

will fail and the return is as you have discovered.
 
Share this answer
 
Comments
Chris Aug2022 28-Sep-22 7:29am    
Yeah, it's around 7:30am here been coding since 4pm. Probably should just go to sleep at this point. Hurricane is starting to hit now(Sarasota, Florida.) Was just trying to study as much as possible before power loss. XD
Richard MacCutchan 28-Sep-22 7:41am    
Yes, I saw the weather warning in our UK news. I hope you come safely through it, it sounds like a big one.

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