← Blog

JavaScript Katas: Count lowercase letters

July, 2020

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]/)) // filter out all lowercase chars
    .length; // 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?

miku86

Hi! I'm Michael 👋 I'm a Mentor & Educator & Senior Web Developer - I help you to reach your (career) goals.