Intro 🌐
I take interesting katas of all levels and explain how to solve them.
Problem solving is an important skill, for your career and your life in general.
You'd better learn to solve problems!
Source
I take the ideas for the katas from different sources and re-write them.
Today's source: Codewars
Understanding the Exercise ❗
First, we need to understand the exercise!
This is a crucial part of (software) engineering.
Go over the exercise explanation again until you understand it 100%.
Do NOT try to save time here.
My method to do this:
- Input: What do I put in?
- Output: What do I want to get out?
Today's exercise
Write a function correctPolishLetters
, that accepts one parameter: inputString
.
Given a string, e.g. Wół
, return a string without the diacritics, e.g. Wol
.
To keep it simple, we'll just care about the lower case characters.
Input: a string.
Output: a string.
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.
- Create a mapping of the characters with diacritics and without them
- Loop over every character
- Check if the current character has a diacritic
- If yes (= has a diacritic), replace it with the character without diacritic
- Return results
Example:
- Input:
Wół
- Iteration 1:
W
has diacritic? =>No
=> not replace it =>W
- Iteration 2:
ó
has diacritic? =>Yes
=> replace it =>o
- Iteration 3:
ł
has diacritic? =>Yes
=> replace it =>l
- Output:
Wol
✅
Implementation (for loop) ⛑
function correctPolishLetters(inputString) {
// mapping for characters
const mapping = {
ą: "a",
ć: "c",
ę: "e",
ł: "l",
ń: "n",
ó: "o",
ś: "s",
ź: "z",
ż: "z",
};
// variable to save result
let withoutDiacritics = "";
// loop over every number
for (const char of inputString) {
// check if mapping has a key with the current character
if (Object.keys(mapping).includes(char)) {
withoutDiacritics += mapping[char];
// if yes, return its replacement
} else {
// if not, return it unchanged
withoutDiacritics += char;
}
}
// return result
return withoutDiacritics;
}
Result
console.log(correctPolishLetters("Wół"));
// "Wol" ✅
Implementation (functional) ⛑
function correctPolishLetters(inputString) {
// mapping for characters
const mapping = {
ą: "a",
ć: "c",
ę: "e",
ł: "l",
ń: "n",
ó: "o",
ś: "s",
ź: "z",
ż: "z",
};
return (
inputString
// split the string into an array
.split("")
.map(
(char) =>
// check if mapping has a key with the current character
Object.keys(mapping).includes(char)
? mapping[char] // if yes, return its replacement
: char // if not, return it unchanged
)
// join the array to a string
.join("")
);
}
Result
console.log(correctPolishLetters("Wół"));
// "Wol" ✅
Playground ⚽
You can play around with the code here
Next Part ➡️
Great work, mate!
We learned how to use a for of
loop, Object.keys()
, includes
and map
.
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?
- What's your simplest solution to add uppercase letters?
Top comments (6)
Nice :)
We can also replace chars that we don't know with either mapped char or with a
?
Hey Kostia,
currently trying to understand your code.
Do you mean
||
instead of??
.Hey, Michael!
Yeah, in this particular case
||
would work as well as??
.Nullish coalesce would be useful if you have a
mapping
that simply drops some char, e.g:Here
||
wont work. But that's just a case I made up 🙂Awesome, thanks for that!
I didn't know that JavaScript added a nullish coalescing operator in ES2020!
Currently running Node 12, I should probably update it, too!
Yeah, JS is getting hard to track :)
Also note
?.
conditional chaining — helps a lot!And they've added more operators just recently (@ stage 4):
||=
&&=
??=
for conditional assignment (dunno why...)article on recent updates: dev.to/hemanth/stage-4-features-5a26
not mentioning other
#
,|>
,{| }
, etc early stage propositions — 🤯Thanks for the summary!
I already use the
?.
in my TypeScript projects,will have a look at the other ones!