The match function returns either an array of matches or null. An if condition checks for Boolean results. So if you use the match function in an if condition, you’ll get true if you return something other than null. For clarity here, you may want to use the Regex.test() function instead since that returns a Boolean.
As written, your code will check the first letter of the provided string and see if it matches your regular expression. If it matches, then it will enter the if block. If not, it will enter the else-if block, but since those are really the only two options (a match or no match), there aren’t any cases (that I can think of) where the else block would be entered.
So for example, if str = ‘consonant’, we check the first if condition. str does not match a, e, i, o, or u so we skip to the next if condition. str.match(/[aeiou]/gi) does equal null, so we enter the else-if block.
If str = ‘thrice’, str does not match a, e, i, o, or u, so we skip to the next if condition. str.match(/[aeiou]/gi) does equal null, so here’s what happens next:
- we convert ‘thrice’ to an array (note that strings already are arrays of consonants).
- we remove the first letter of the [‘t’, ‘h’, ‘r’, ‘i’, ‘c’, ‘e’] and store it, so the variable letter now equals [‘t’] and arr now equals [‘h’, ‘r’, ‘i’, ‘c’, ‘e’].
- we join the elements of arr (‘hrice’), then concatenate [‘t’] to the previous (‘hricet’), then concatenate ‘ay’ to the previous (‘hricetay’, which is not exactly what we want to return).
Otherwise, your code is looking great, @Neralizer. Keep up the good work!