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:
- Input: What do I put in?
- 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.
- Loop over every character
- Check if it is a lowercase letter
- If yes, then increase count of lowercase letters by 1
- 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?
Top comments (0)