Today was all about practice, and I decided to challenge myself on Codewars. It was an exciting experience solving coding exercises, particularly one titled “Disemvowel Trolls”. Here’s how it went:
Challenge Title: Disemvowel Trolls
Instruction
Trolls are attacking your comment section! To neutralize the threat, remove all the vowels from the trolls’ comments.
Your task is to write a function that takes a string and returns a new string with all vowels removed.
Example
Input: “This website is for losers LOL!”
Output: “Ths wbst s fr lsrs LL!”
Note: For this kata, the letter y is not considered a vowel.
Solution
Here’s the solution I came up with:
function disemvowel(str) {
const vowels = ['a', 'e', 'i', 'o', 'u']; // List of vowels
let container = ""; // To store characters that are not vowels
for (let i = 0; i < str.length; i++) {
if (!vowels.includes(str[i].toLowerCase())) {
container += str[i]; // Add non-vowel characters to the container
}
}
return container; // Return the string without vowels
}
Explanation
Define Vowels
I created a variable vowels that holds an array of all vowels: ['a', 'e', 'i', 'o', 'u']. This is the reference point for determining if a character is a vowel or not.
Initialize a Container
A container variable is used to collect all characters that are not vowels.
Iterate Through the String
Using a for loop, I iterated through each character of the input string (str).
Check for Vowels
For each character, I converted it to lowercase (str[i].toLowerCase()) to make the check case-insensitive. I used the .includes() method to verify if the character is a vowel.
Add Non-Vowels to Container
If the character is not a vowel, it gets appended to the container.
Return Result
After the loop finishes, the container contains the string with all vowels removed, and it is returned as the output.
Key Insights
I realized the importance of handling case sensitivity when processing strings. Converting characters to lowercase ensures consistent comparisons.
Using arrays and methods like .includes() makes checking for specific conditions (like whether a character is a vowel) much simpler and cleaner.
console.log(disemvowel("This website is for losers LOL!"));
// Output: "Ths wbst s fr lsrs LL!"
console.log(disemvowel("Hello World!"));
// Output: "Hll Wrld!"
Reflections
This exercise reinforced the importance of problem-solving skills in programming. Breaking down the challenge into smaller steps made it much easier to tackle.
I’m excited to dive into more challenges tomorrow.
Day 7, here I come! 🚀