Day 30 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. And I finally have made it to day 30!! Wow, what a journey!

Today I did: https://www.codewars.com/kata/single-character-palindromes

Time: 8 minutes

What I’ve learnt:

I totally thought this would be impossible! But then I thought about it for a minute, and I was like, hey, that should be easy.

We had three things to check, return “OK” if it is currently a palindrome. That is easy 🙂 string === string.split("").reverse("").join("")

Onto the next challenge. Check if we remove just one letter, will it become a palindrome? If yes, return “return one”.

In a for loop I created a new variable holding the original strings value split into an array (so we don’t obstructively mess up our original array as we iterate), then spliced off the current index (i), and only one element, newS.splice(i, 1). Then checked if it was a palindrome the same way I did above (shoulda made a method for this if I used it twice!) and if it did I returned remove one so that it would break out of the loop since we have the answer.

And the last line says return "not possible" since it has not passed those first two tests.

Score!

What I learned from other people’s code:

I see someone using the do...while. I don’t particularly love their code, but I do like that syntax. It is very readable.

A lot of people made an external function to check if palindrome. That makes sense. Although quite a few actually ran loops to check if it was a palindrome.

Ah, I think if I would have used substring I would not have had to assign to my variable and then remove the letter from it.

I can’t believe I made it this far. It was a great learning journey. I now feel like I can do anything in the world to strings, lol. Have learned new ways to think. Problem solving is coming easier. And I see in how many ways I can grow! Looking forward to an exciting journey as I advance in my programming path. The journey never ends, and the learning never stops!

Day 29 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/help-the-bookseller

Time: 24 minutes

What I’ve learnt:

This was actually a nice challenge. Not too brain twisting, but just a nice problem to tackle.

The first thing I thought to do was to create an object with the category letters (from the second array) with reduce, and Object.assign to give me an object of letters with the value of 0.

I then did a forEach on the through the art array (first parameter), and checked if the first letter art[0] was a key in the object created by the reduce, then I added the price onto it. I was imagining myself in an interview, and thinking if I would go and play with regex to get the numbers out of these strings "ABART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60" and thought no way, I just went with `parseInt(art.split(” “)[1])` , where I split the string by the space they all had, and just grabbed the 1st index of the newly created array.

Now I have an object with the correct values, but I need to return a string like so: (A : 20) - (B : 114) - (C : 50) - (W : 0)

I used `for (category in categoryObject)` where category gave me the key and `categoryObject[category]` the value. With interpolation I pushed this into an array outside of the for in loop, and after I returned that array with join(" - ").

Flying green!

What I learned from other people’s code:

Again no one liners here (except for one person, but if we copy the code to an editor with text wrap it’s on 5!).

Someone just used a forEach to add to the outer object, and therefore they did not need to worry about returning with an Object.assign. Keeping it simple! Awesome! They also chose to split the string rather than use regex.

I see I could have mapped my last array, instead of pushing it into an outer array and joining that. Duh!

Someone else used alistOfArt.reduce inside thelistOfCat.map to get the string all at once. It’s hard to know what is better to spread out, so each method is doing it’s own thing and what to combine. But great to see how it can be so compact.

 

Tomorrow is 30!!!! Who can believe it?!?!?!

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!

Day 19 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/counting-duplicates/javascript

Time: 10 minutes

What I’ve learnt: First things first I made all letters lowercase, and split it into an array of sorted letters. I wanted to use a reduce, and decided I needed to keep track of the previous letter, as well as letters that were already marked duplicate, because I need to make sure not to duplicates multiple time on the same letter. So I passed in the initial value of the reduce to look like `{prevLetter: “”, dupsArray: []}`.

Then in each iteration of the reduce I checked if the previous letter was equal to the accumulator.prevLetter && that it was not included in the accumulator.dupsArray, if both these conditions were met I added it to the accumulator.dupsArray. Then I reset the prevLetter to the currentLetter,  and returned the new accumulator.

Once this finished I just needed to return the reduce.dupsArray.length. All done 🙂

What I learned from other people’s code: It is interesting how people used regex to check for dups. `match(/([^])\1+/g)` I’ll have to play with that on a regex site!

And onto the next 🙂

Day 18 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/sum-of-a-nested-list/javascript

Time: 60 minutes

What I’ve learnt: So I spent time doing this one (even though it is a kata 7, it took me a while because I wanted to do it without too many conditionals. After some trial and error of trying to create a reduce method that would flatten the array (which on some conditions would need to have been done recursively because they were nested), I decided to simple join the array into a string.

This gave me a string with commas, so I just ignored the commas with a match using some regex. Yay!! I finally used regex! Match returned me an array of numbers so I can reduce it down to it’s sum. I needed to add in a ternary to check if the array converted to a string without commas was undefined , because otherwise calling reduce on that would fail.

What I learned from other people’s code: I saw a lot of recursion, where in their reduce they checked if Array.isArray, and then if it was they did recursion on that array to the entire method, which built up a stack for the reduce to sum up. And someone used lodash _flatten!

Have a good one!

Day 17 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-array-revisal/javascript

Time: 30 minutes

What I’ve learnt: This was done at my weekly javascript meet-up. Thanks for everyone’s awesome ideas! So again, string manipulation. I will expect to see regex in the solution. 🙂 But at this meeting, we were using our more comfortable conditionals. Pretty standard manipulation, and adding onto a string within a for loop to get this kata solved.

Here was my solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
function dup(stringArray) {
  let result = [];
  for (let i = 0; i < stringArray.length; i++) {
    let previousLetter;
    let newStrings = stringArray[i].split('')
    
    newStrings.forEach(letter=> {
      if (!previousLetter) {
        result[i] = letter;
      } else if (letter !== previousLetter) { 
        result[i] += letter;
      } 
      previousLetter = letter;
    })
  }
  return result;
};

 

What I learned from other people’s code:

So as I expected, I see some replace with regex 🙂 This is definitely something we are going to have to do at a future meet-up. It’s amazing how they can conquer so much, in so little code. But until I start playing with it more it feels so untouchable! Add to the TODO: regex!!

Have a good night all 🙂

Day 15 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/the-maximum-sum-value-of-ranges-simple-version

Time: 10 minutes

What I’ve learnt: This kata I started with a for loop on the ranges. I then created a new array based on the start and end range (range[0], range[1]) with slice as not to manipulate the old array (because we will use it on each range!). Then I reduced this new array to get the total. (Hurray for using reduce!). I then created a variable outside the loop called maxSum, and set it equal to nothing (originally I had it equal to 0 but that broke my code as sometime it is a negative number), then I had a conditional checking if the current sum (from the reduce) was greater than the maxSum (or if maxSum was undefined for the first iteration), and if sum was greater replace maxSum with the current sum. And as the final return before the function closed out I returned maxSum. I knew I could have wrapped the entire loop (instead of the for loop) in a filter, but I felt the code was less readable to I kept it in a for loop.

What I learned from other people’s code:

Some people really love to concise everything down to one line. And I really thought it was amazing. But I think I would rather keep my code readable, so when I came back to it after some time, I would be able to understand it quite quickly. Some mapped it into an array (instead of my outer for loop) and called Map.max on the map, that is cool 🙂 Slice and reduce was used in most, so I’m feeling happy with my code.

Half way through! See you tomorrow!

Day 14 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/reverse-words/javascript

Time: 15 minutes

What I’ve learnt: So this was a relatively easy kata, something I have done before. And I put in my solution and it wouldn’t pass, and was quite confused for a few minutes, until I realized there were no initial tests and I had to create them! So I used Test.assertEquals(functionToBeCalled, ExpectedReturn, ‘Description of test’). I used a map here, and joined them right back to be a string after my map, instead adding to a variable holding through the string as it loops.

Here was my solution:

1
2
3
function reverseWords(str) {
  return str.split(" ").map((word)=> {return word.split("").reverse().join("")}).join(" ")
}

 

What I learned from other people’s code:

I think most people did almost identical to mine. In fact there are 328 grouped as having done the kata like that. Someone others split the whole thing without space (by letters), reversed everything, then joined back together, split again by space and then did one more reverse to put the words back in order. That is definitely an out of the box way to think about such a problem.

I hope my javascript is improving! Until tomorrow 👋

 

Day 13 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/deep-freeze/javascript

Time: 30 minutes

What I’ve learnt: So we had to freeze the first object and then the inner ones, so recursion was definitely called for. I used the javascript for in loop which is a nice change from the regular for loop which would not have worked well here. I got confused first trying to check if the item was an object, until I realized I needed to check the value of the object itself, and not the item alone (object[item]).  That’s what took me the longest time. I also tried to make it more concise after with reduce or map, but it really wasn’t necessary or cleaner, so left as is.

Here was my solution:

1
2
3
4
5
6
Object.deepFreeze = function (object) {
  Object.freeze(object)
  for (var property in object) {
    if (object[property] instanceof Object) Object.deepFreeze(object[property])
  }
}

What I learned from other people’s code:

Seems like recursion was common practice here. typeof is another way to check for an object as opposed to instanceOf Object. Happy to see no one used reduce or filter. 😉

Can’t wait for tomorrow’s challenge 🙂

Day 11 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/find-the-divisors/solutions/javascript

Time: 20 minutes

What I’ve learnt: This was pretty straightforward code-wise. I started my for loop though at 2 because 0 and 1 are not something I would need to check for to see if a number has divisors. And we only need to check until the integer/2, instead of the entire integer.

Here was my solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function divisors(integer) {
  const divisors = []
  
  for (let i = 2; i <= integer /2; i++) {
    if (integer % i === 0) {
      divisors.push(i)
    }
  }
  return divisors.length ? divisors : `${integer} is prime`
};

 

What I learned from other people’s code: I feel like some people tried to shove their code together to take up less lines, but it made it much less readable. Overall seems everyone did similar.  No new fancy javascript syntax on this one!

See you tomorrow!

Copyright Loving to Code 2025
Tech Nerd theme designed by Siteturner