Day 2 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/5898b4b71d298e51b600014b

Time: 25 minutes

I’ve used slice today!  It really helped keep this concise without having to keep creating variables, since slice does not mutate the original.

Here was my solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function sortTheInnerContent(words) {
  
  return words.split(' ').map(word => {
    let newWord = word[0]
    word.length > 2 ? newWord += word.slice(1,word.length-1).split('').sort().reverse().join('') : null
    if (word.length > 1) newWord += word[word.length-1]
    
    return newWord
  }).join(' ');
}

 

What I learned from other people’s code:

I really have to start getting more comfortable with regex. It’s amazing how you can do so much with it. Although I do find it harder for the next dev to jump right into the code.

I see people use .substr which is not something I have seen yet. That is another nice way to non-destructively grab a part out your string.

const string = “Hello”

string.substr(2, 2) // “ll”

string // “Hello”

Ready for the next Kata!!

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