Skip to content
Advertisement

Find how many times a char is included into an array of names

I’m trying to do the following exercise in JavaScript: I need to return the number of times the letter ‘l’ is used inside an array of names.

This is an example of the array I have to use:

JavaScript

Note that the letter ‘l’ might be repeted inside a single name.

Is there any JS method I could use to do this ? I’ve tried with indexOf but couldn’t make it work

Advertisement

Answer

Try this:

JavaScript

This splits the string everytime you see the char you want and returns the number of strings after splitting at the delimiter minus 1 which is exactly what you want. Do this after converting the array of strings to a single string. myStr = myStrArray.toString();

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement