JavaScript Katas: Count lowercase letters

Intro 🌐

Problem solving is an important skill, for your career and your life in general.

That's why I take interesting katas of all levels, customize them and explain how to solve them.


Understanding the Exercise❗

First, we need to understand the exercise! If you don't understand it, you can't solve it!.

My personal method:

  1. Input: What do I put in?
  2. Output: What do I want to get out?

Today's exercise

Source: Codewars

Write a function amountOfLowercaseLetters, that accepts one parameter: inputString.

Given a string, e.g. "aB1c", return the number of lowercase letters in this string, e.g. 2:


Input: a string.

Output: a number.


Thinking about the Solution πŸ’­

I think I understand the exercise (= what I put into the function and what I want to get out of it).

Now, I need the specific steps to get from input to output.

I try to do this in small baby steps.

  1. Loop over every character
  2. Check if it is a lowercase letter
  3. If yes, then increase count of lowercase letters by 1
  4. Return count of lowercase letters

Example:

  • Input: "aB1c"
  • Iteration 1: lowercase letter? true => Increase count of lowercase letters by 1
  • Iteration 2: lowercase letter? false => Do nothing
  • Iteration 3: lowercase letter? false => Do nothing
  • Iteration 4: lowercase letter? true => Increase count of lowercase letters by 1
  • Output: 2 (count of lowercase letters) βœ…

Implementation (for) β›‘

function amountOfLowercaseLetters(inputString) {
  let count = 0;

  // loop over every char
  for (const char of inputString) {
    // check if it is lowercase
    if (char.match(/[a-z]/)) {
      // if yes, increase count
      count += 1;
    }
  }

  return count;
}

Result

console.log(amountOfLowercaseLetters("aB1c"));
// 2 βœ…

console.log(amountOfLowercaseLetters("123"));
// 0 βœ…

Implementation (functional) β›‘

function amountOfLowercaseLetters(inputString) {
  return inputString
    .split("") // convert into array
    .filter((char) => char.match(/[a-z]/)).length; // filter out all lowercase chars // take the length
}

Result

console.log(amountOfLowercaseLetters("aB1c"));
// 2 βœ…

console.log(amountOfLowercaseLetters("123"));
// 0 βœ…

Implementation (global regex) β›‘

function amountOfLowercaseLetters(inputString) {
  return (inputString.match(/[a-z]/g) || []).length;
}

We use the g flag to return all matches. Because null would get returned if there wouldn't be a match, we add an [] to use length.

Result

console.log(amountOfLowercaseLetters("aB1c"));
// 2 βœ…

console.log(amountOfLowercaseLetters("123"));
// 0 βœ…

Playground ⚽

You can play around with the code here


Next Part ➑️

Great work!

We learned how to use for of, match, filter and length.

I hope that you can use your new learnings to solve problems more easily!

Next time, we'll solve another interesting kata. Stay tuned!


If I should solve a specific kata, shoot me a message here.

If you want to read my latest stuff, get in touch with me!


Further Reading πŸ“–


Questions ❔

  • How often do you do katas?
  • Which implementation do you like more? Why?
  • Any alternative solution?