简体   繁体   中英

How can I loop through an array of letters and return the first consecutive consonants in the array?

I'm trying to remove only the first consecutive consonants from the array so it returns ["e","l","l","o"]

arr=["h","e","l","l","o"]
vowel=["a","e","i","o","u"]
consonants=[];

for ( i in arr){
    if(vowel.indexOf(arr[i]) =-1 ) {
       consonants+=arr[i];
       arr= arr.slice(consonants.length)
    }else { return arr }   

}

I might have interpreted your question wrong, but I took it as meaning "return an array with all the leading consonants removed".

Anyway, this seems like a pretty good use-case for regular expressions:

var arr=["h","e","l","l","o"]

// hat-tip to @minitech in the comments for improving this line...
var result = arr.join('').replace(/^[^aeiou]+/, '').split('');

console.log(result);
// [ 'e', 'l', 'l', 'o' ]

Explanation:

  • arr.join('') converts the array into a string
  • .replace(/^[^aeiou]+/, '') removes any leading non-vowel characters
  • .split('') converts the result into an array of characters

In the loop here,

for ( i in arr){

you should use a normal for loop instead; for in is for iterating over properties and iterates in an arbitrary order .

In the condition here,

if(vowel.indexOf(arr[i]) =-1 )

you'll need to use == to check for equality; = is only for assignment. ( === is also a good option, for strict equality.)

In the statement here,

consonant+=arr[i];

you're converting consonant from an array to a string the first time around (assuming consonants was just a typo). Either is fine if you're only using its length property, but you should be consistent and either initialize it with the empty string '' or append to it using the .push() method.

In the reassignment of arr here,

arr= arr.slice(consonant.length)

you're losing the original object being enumerated, which will skew your indexes for future iterations. You're also slicing consonant.length off each time, which will result in total removals of 1, 3, 6, 10, and so on characters. It's probably best to break out of the loop and do the slicing once at the end.

You should also declare your variables if you aren't already. All told,

var arr = ["h", "e", "l", "l", "o"];
var vowels = ["a", "e", "i", "o", "u"];
var consonants = [];

for (var i = 0; i < arr.length; i++) {
    var c = arr[i];

    if (vowels.indexOf(c) == -1) {
        consonants.push(c);
    } else {
        // using `i` in place of `consonants.length` here would work just as well
        return arr.slice(consonants.length);
    }
}

Your actually preety close the correct function would look like

arr=["h","e","l","l","o"];
vowel=["a","e","i","o","u"];
consonants=[];

for (i = 0; i < arr.length; i++){
    if(vowel.indexOf(arr[i]) == -1 ) {
       consonants.push(arr[i]);
       arr= arr.slice(consonants.length)
}else { return arr }   

}

Your for loop was a little off and your constonant variable was named 2 different things in your function...you have constonant and constanants

Try array.filter - like this:

var arr=["h","e","l","l","o"],
vowel=["a","e","i","o","u"],
first_consonant_found = false;

var consonants = arr.filter(function(ele){
    if (vowel.indexOf(ele) == -1 && !first_consonant_found){
        first_consonant_found = true;
        return false;
    } else {
        return true;
    }

})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM