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 28 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/check-if-two-words-are-isomorphic-to-each-other/javascript

Time: 18 minutes

What I’ve learnt:

It took me a few minutes to comprehend what this exercise was asking for. It wants us to check that each time a letter is used, the value should always be used again in the same place of the second string. And this value on the second string cannot be assigned to any other letter (I only realized this after the extended tests were not passing!).

First things first, if the a and b lengths are not the same, they are clearly not a match — return false and knock that out the door!

I decided the best way to keep track of what the corresponding matching letter on the a string to the b string would be an object. So I defined that object. Then started up a for loop, checked it therelationshipMappingObject[a[i]] does not yet exist && the Object.values(relationshipMappingObject) does NOT include b[i] (meaning it has not yet been assigned to another letter) then let’s go ahead and assign it to our Object.

Then I checked `if (relationshipMappingObject[a[i]] !== b[i])` and returned `false` . If we made it through the whole for loop, then after the for loop without returning false I just put return true.

Green lights! Wohoo! Not as bad as I thought. Took longer to understand what was expected than to code.

What I learned from other people’s code:

One person who did it on one line, I can’t even see what he did! I have moved past being the one liner cool man! 🙂

What others did was iterate through each string (a and b) separately and create an object, with the key being the letter, and the value being the index. Then checking if the second string held those same indexes. I gotta relook at that and see how that accounts for duplicates.

See ya tomorrow!

Day 27 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/chess-fun-number-1-chess-board-cell-color/javascript

Time: 10 minutes

What I’ve learnt: I knew I was going to have to check the same logic on both cells passed in, I thought creating an external function that would give me the color, then I can just call return checkColor(cell1) === checkColor(cell2)

Now onto the checkColor function! If the charCode of the cell[0] (the letter portion of the spot on the board) is even, we know that if the cell[1] (the number part of the spot on the board) is also even we are on “brown”, otherwise “white”, and the opposite is true if the cell[0].charCodeAt() is odd.

I used two ternaries, inside an if/else, and all was green. 🙂

const charCode = cell.toUpperCase()[0].charCodeAt() if (charCode % 2) return cell[1] % 2? "brown" : "white" else return cell[1] % 2? "white" : "brown"

What I learned from other people’s code:

I see a lot of people used a separate function to check the cell color like I did.

Someone used a bitwise operation `&1` which takes the number and converts it to Binary 1, which means if it’s odd it will return 1, and if it’s 0. I don’t totally get it, and probably won’t use it at this point, but at least I will recognize it. More info is here.

Other than that, it seems everyone used similar logic to mine.

Can’t beleive there are only 3 days left! 3…2…1!

Day 26 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-tops/train/javascript

Time: 7 minutes

What I’ve learnt: I decided there must be some math involved so we don’t have to continuously iterate and check to see if the current one is an even number (as opposed to odd which would be the down turn), so I looked at all the last index and the next index and wanted to see what was in common. So basically the common value was that the current index minus the last returned index was 4 greater than the previous value’s total (which was added 4 to in the past iteration).

So to deal with the blank string, and just one letter string, I started an array with const msgArray = [msg[1]]and then I was able to start my for loop with an index of 6, the next possible iteration.

Thenif (i - lastIndex === lastTotal + 4) so i is 6, and the lastIndex and LastTotal were both predefined with 1 (because remember we did the first round already which was the index of 1. So 6-1 === 1 + 4, and therefore we hit the conditional, then need to reassign the lastTotal to +=4, push the current index of the msg into our msgArray, and set the lastIndex equal to the current index.

Then to give the expected return, I reversed the msgArray and joined it. Green lights! Woohoo!

What I learned from other people’s code: I expected to see some crazy math on how someone can do this quicker, and I did! Let me try to understand what they are doing.

So they counted up one from the previous index, times that number by 4, and then added 1. A lot of people used that increment by += 4. Someone used this (2*i*i)-i+1 There was a lot of recursion here with the same idea of my math. Wonder when it is better to use recursion, and when a for loop will justify. I’ll need to read up on it. 

This is turning out to be quite the journey! See you tomorrow 🙂

 

Day 25 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/are-they-the-same/javascript

Time: 10 minutes

What I’ve learnt: So I was given 2 arrays, one with the number only, and one with the number * number. I wanted to take the number * number array and map over it and use regex to match up until the *  `(\d+?(?=\*)` but since it already did the multiplication and squared the number before I had a chance, I had to map through the number array and change that to number * number with a map.

Then I sorted and split these into strings to test if they were equal, because Objects and Arrays are never === unless they are using the same space in memory. And since they were created individually, I needed to split them into a string to compare them.

What I learned from other people’s code: Interesting someone compared the array’s against each other with the .every() we learned about the other day!

I thought this would be much harder than it was. Until tomorrow!

 

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 23 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/which-are-in

Time: 15 minutes

What I’ve learnt: Another string iterator. This time with two arrays of words, and I had to return the words that were found inside the words from the second array. Somewhat similar to yesterday’s.

I mapped through the second array which contained the bigger set of words, and inside that map ran a filter to check which if the outer mapped words, includes the inner filtered words. I then was left with an array with duplicates, and empty values. So I created a reduce that checked to make sure it was not empty && not included in the accumulator array, if this was true I returned [...acc, i[0]] (because of the filter inside the map, each word string was subnested in it’s own array), and if it was not true, I just returned the acc.

Then I sorted the return. I did not use variables, I just called the function straight on what the previous function returned, but instead of keeping it on one line, I put it on multiple lines for better readability.

What I learned from other people’s code:

I see someone use a Array.prototype.some(), I have never seen this before. I tried the every, but that didn’t work, because it was only partly equal. So the .some() seems to be what I was looking for. Seems to be similar to the ruby any. Others used .match(), to see if the largerWord.match(smallerWord). Interesting, because normally I’ve seen that used with regex.

Array.prototype.find() also seems it is more useful than filter, because it will stop as soon as it finds one, which will avoid our duplicate issue, and will keep processing time down.

Someone else converted the larger string array into one long string separated with spaces by word, and then was able to check for indexOf on the first word. Very interesting, and this was only one filter! No inner loops.

It’s amazing how people think!

I’ve learned a lot today, looking forward to tomorrow!

Day 22 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/how-many-feelings/javascript

Time: 15 minutes

What I’ve learnt: Let me start by saying, Javascript needs a uniq built in function! I spent 7 minutes on the method, and then another 7 minutes on looking for a Javascript uniq function, before having to build my own. I tried to use the lodash _.uniq(array) but it didn’t work!

Anyways, so we had to check a string for particular words in an array. So I used a forEach loop to iterate through the array of words, and in the forEach I filtered on the string that was passed in (by using .split("") to turn it into an array), and it was filtered if a letter in the the string was included in the wordArray.

I then compared this newly created variable’s length (with my external uniq function) against the word.length, and if they were equal then we clearly had all the letter’s in the string, and upped my counter of matchedFeelings to keep track of the amount of words found.

My uniq method took in an array, reduced over the array and if the accumulator did not include the current letter we are currently iterating over then we pushed it into the accumulator, and returned the accumulator whether or not it includes the character.

I then checked with a ternary if my counter was === 1, if it was 1 I returned “1 feeling”, and if not I returned ${counter} feelings.

What I learned from other people’s code:

Many have used .every on an array. I have never heard of this. What this method does it checks if every one of these elements meet a certain condition. array.every(<condition to be met>). So I would do string.split.every(i => string.includes(i)) I would have been able to increment my counter if that was true, instead of needing my uniq. Nice to know Javascript has some helpful built in methods!! Every, every, every, need to remember this one!

Until tomorrow! 

Day 21 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.
Time: 150 minutes (I know, this is long!)
What I’ve learnt: So to be honest, I thought this would be way simpler than it seemed, but since I started I had to go through with it. The math on getting a common denominator is much harder in programming than it was in grade school!
So the first thing I did was create a reduce with in an initial acc `[1, []]` of  that iterates through the lst passed in (which is an array of fractions) and multiplies the denominator (fraction[1]) to the acc[0], and pushes the denominator into the acc[1] array. So at the end of this array I have the product of all the denominators multiplied by each other, and an array holding all the possible denominators.
I then created a getLCD function that takes in the product of all the denominators, and the array of all the denominators that I got from my reduce. I sorted the denominator array, and then created a filter inside of a for loop based on the arrayOfDenominators.length that checks if each of the denominators is divisible by that denominator we are currently iterating through in our for loop. If they all are divisible, then I divided the greatest common denominator by that number, and reassigned that gcd that was originally passed in as a parameter. I then iterated through a newly created array of 2-9, and checked again to see if we can lower the denominator further.

Now that we have the LCD, I used a created a function `refactorFactions` that takes in the LCD, and a specific fraction, and it divides the lcd/denominator assigned to a variable multiply, and then returns a string or the numerator * multiply and denominator * multiply.

The above function was called in a reduce that added the strings to the accumulator to return the final string.

Pheeew. All green. I just need to show you after all this hard work!!
What I learned from other people’s code: So I knew there would be a better way to get the LCD with recursion, but even now I can’t fully understand it. Here is a nice blog on it. https://www.w3resource.com/javascript-exercises/javascript-math-exercise-10.php. I need to do this line by line, but my brain is totally fried! So I’m going to have to relook at this, and maybe blog about it in a future post.
What a day! 😰 But at least there was a happy ending!
Copyright Loving to Code 2025
Tech Nerd theme designed by Siteturner