Day 24 of 30 of Codewars – Javascript

I’ve challenged myself to 30 days of codewars, one a day, time myself, and learn how I could have done it better.

Today I did: https://www.codewars.com/kata/string-reduction/javascript

Time: 20 minutes

What I’ve learnt:In my weekly Javascript coding group we conquered this one. We used replace here! Even if it was used without regex 🙂 and directly mutated the string that was passed in. Awesome to code with everyone!

Here was my solution:

1
2
3
4
5
6
7
8
function solve(a, b) {
  if (b.length >= a.length) return 0
  for (let i = 0; i < b.length; i++){
    if (!a.includes(b[i])) return 0
    else if (i === b.length-1) return a.length - 1
    else a = a.replace(b[i], "")
  }
};

 

What I learned from other people’s code:

It was pretty interesting how people used the spread operator ... to spread out the string in an array and loop through it that way. Cool to see how others think 🙂

Have a good one!

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright Loving to Code 2025
Tech Nerd theme designed by Siteturner