I know this is massively illegible compared to your beautifully constructed routine (which might well be how I'd write it because I'm all for legibility) but the short version might be coded as:
str is the target decomposed into a character-array
the a represents the accumulator in the callback that is initialized to be a counter of 0 which is at the end as the second argument to Array.prototype.reduce.
the c represents the character (letter)
the body of the callback reads as such -- a (accumulator/counter) + 1 if the character, c, is found, and 0 otherwise using a ternary operator expression.
I love Array.prototype.reduce, too, but this is not expressive LOL.
Using good variable names here is important IMO:
letstr="foobar",defaultCount=0;// reduces vowel look-up to O(1) compared to O(n) where n is the # of vowelsconstvowels=newSet("aeiouAEIOU".split(""));str.split("").reduce((vowelCount,character)=>{returnvowelCount+(vowels.has(character)?1:0);},defaultCount);>3
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I know this is massively illegible compared to your beautifully constructed routine (which might well be how I'd write it because I'm all for legibility) but the short version might be coded as:
Yeah, this is no bueno. LOL.
For those trying to understand...
str
is the target decomposed into a character-arraya
represents the accumulator in the callback that is initialized to be a counter of0
which is at the end as the second argument toArray.prototype.reduce
.c
represents the character (letter)a
(accumulator/counter) +1
if the character,c
, is found, and0
otherwise using a ternary operator expression.I love
Array.prototype.reduce
, too, but this is not expressive LOL.Using good variable names here is important IMO: