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 12 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/dont-rely-on-luck/javascript

Time: 8 minutes

What I’ve learnt: It took me about a minute to realize that we need to override a javascript built in function here. And about 7 minutes to realize exactly how to do this. I tried first by recreating the function, but I was getting an error creating a function with the name Math.floor, and then I tried assigning it to a variable with a const, and then realized that won’t work because it has already been created! So I just did Math.floor = and that worked the magic!

Here was my solution:

1
2
3
4
5
var guess = "Sorry. Unlucky this time."

Math.floor = () => {
  return "Sorry. Unlucky this time."
}

 

What I learned from other people’s code:

Well, everyone did this quite similarly, there is not much change you can do. I guess I could have put my arrow function on one line to knock down some of the lines of code, and the return.  I also did not realize that you just had to have a static return, and that it did not need to return that particular string.

And, onto the next!

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!

Day 10 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/5839edaa6754d6fec10000a2

Time: 25 minutes

What I’ve learnt: This one I did in my weekly javascript study group with a few coding buddies. I love doing kata together as we get to discuss different ideas together 🙂

We used functions we have all used in the past to achieve this solution, inside our for loop. Nothing too new this time.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
function findMissingLetter(array) {
  let nextLetter = 0;
  
  for (let i = 0; i < array.length - 1; i++) {
    let currentLetter = array[i].toLowerCase().charCodeAt();
    nextLetter = array[i + 1].toLowerCase().charCodeAt();
      
    if (nextLetter && nextLetter !== currentLetter + 1) {
      return String.fromCharCode(array[i].charCodeAt() + 1 )
    }
  }
}

 

What I learned from other people’s code:
Although not required from the kata, I like it how someone used

throw new Error(“Invalid input”)

The sound of that function totally matches what it does! It’s awesome!

Otherwise I feel people have reached their solution similarly to ours. I do like MaximumJoe’s one line solution using reduce. Gotta start reducing too!

Until tomorrow!

Day 9 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-parity-outlier/

Time: 25 minutes

What I’ve learnt: So I made an inner function isEven to keep my code a bit DRYer, as this is something I am checking over and over. I’ve used quite a large condition for my ternary for integersType, but I think it does the job. 🙂

Here was my solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
function findOutlier(integers){

  function isEven(num){
    return num % 2 === 0
  }
  
  let integersType
  (isEven(integers[0]) && isEven(integers[1])) || 
  (isEven(integers[1]) && isEven(integers[2])) || 
  (isEven(integers[0]) && isEven(integers[2])) ? integersType = "even" : integersType = "odd"
  
  for (let i = 0; i < integers.length; i++){
    if (integersType === "even" && !isEven(integers[i]) ){
      return integers[i] 
    } else if (integersType === "odd" && isEven(integers[i])){
      return integers[i] 
    }
  }
}

 

What I learned from other people’s code:

Interesting how people used filter to gather all the even and odd numbers separately, and see which one had only one number, and return that. So although I know how to use filter and reduce, I don’t use it often enough.

Another day of learning, a better programmer 🙂

Day 8 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/calculate-string-rotation/javascript

Time: 20 minutes

What I’ve learnt: Back to string manipulation at codewars!  So I got a lot of tests passing pretty quickly, but to knock out the ones that should be returning -1  took me a few attempts at adding things to my conditional.

Here was my solution:

1
2
3
4
5
function shiftedDiff(first,second){
  var firstLetterPosition = second.indexOf(first[0])
  if (firstLetterPosition === -1 || (first[1] !== second[firstLetterPosition +1] && firstLetterPosition !== first.length-1 ) || (first[first.length - 1] !== second[firstLetterPosition -1] && first !== second && first[1] !== second[0])) return -1
  return firstLetterPosition
}

 

What I learned from other people’s code:

I should have spent 5 more minutes thinking about what the failing condition was, instead of continuously adding conditionals to get my codewars passing. Seriously all I needed is if first.length != second.length… duh!

The way they did this was seriously interesting (thanks Daniel for explaining by the way!). They added up the two strings (of the second), for example in this test where “coffee” was shifted over 2:

shiftedDiff(“coffee”, “eecoff”) => 2

So second + second would be “eecoffeecoff” and checked at what index does the first (“coffee”) show up, and return that number. Amazing thinking!!

I did this similarly but just by looking for the first letter, had I looked for the whole word if it didn’t exist it would know right there.

Looking forward to tomorrow! Hopefully will do this kata before midnight 😉

Day 7 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/change-it-up/javascript

Time: 30 minutes

What I’ve learnt: Another string manipulation here. So you can be sure I will be using charCodeAt() though I had to keep in mind that we will need to go from z to a instead of continuing on with the charCode numbers.

I used the Number.isInteger here to  check if it is a number instead of checking if it is equal to the parseInt equivalent, because I heard that can run into problems and this is the best way to check.

I used many conditionals as I worked through this, and I’m sure there is a better way if I spent more time on this. I did use the one line if statements without the brackets, I feel like they look cleaner.

Here was my solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
const vowels = ["a", "e", "i", "o", "u"]

function changer(string) { 
  changed = ""
  string.split("").forEach(letter => {
    var nextLetter = String.fromCharCode(letter.charCodeAt()+1)
    if (letter == "z" || letter == "Z") nextLetter = "a"
    if (Number.isInteger(parseInt(letter))) changed += letter
    else if (letter == " ") changed += " "
    else if (vowels.includes(nextLetter.toLowerCase())) changed += nextLetter.toUpperCase()
    else changed += nextLetter.toLowerCase()
    })
  return changed 
}

What I learned from other people’s code:

So again people have used regex here to seriously concise their code. I have got to start using the .replace with regex.

I see someone using waay more conditionals than me, and with brackets aaaaannnd ;!! Did I ever mention how ugly those unnecessary ; are?  But that’s just my opinion 😉

By the way I have figured out what ~~ does, (thanks to John!). It’s not =~~, that was just because they were assigning it to a variable. It converts it to a number in case it was not, and then rounds it down.

Another new thing learned 🙂 Can’t wait for tomorrow!

Day 6 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/human-readable-time/javascript

Time: 20 minutes

What I’ve learnt: So I know I could have done this with less conditions, but I was more focused today in trying to accomplish this in a timely way. So there was a lot of copy and paste code, just using a bit different math. Maybe I could have created an outer function I could have passed the math into to keep my code DRYer. I’m never sure how much I should be doing to keep these kata cleaner and cleaner. For now focusing on getting through the challenges in a timely way.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
function humanReadable(seconds) {
  const time = {hours: "00", minutes: "00", seconds: "00"}
  function convertSeconds(seconds){
    if(seconds < 60){
      if (seconds < 10) time.seconds = `0${seconds}`
      else time.seconds = seconds
    } else if(seconds < 3600){
      const minutes = Math.floor(parseInt(seconds)/60)
      if (minutes < 10) time.minutes = `0${minutes}`
      else time.minutes = minutes
      convertSeconds(parseInt(seconds) % 60)
    } else {
      const hours = Math.floor(parseInt(seconds)/3600)
      if (hours < 10) time.hours = `0${hours}`
      else time.hours = hours
      convertSeconds(parseInt(seconds) % 3600)
    }
  }
  convertSeconds(seconds)
  return `${time.hours}:${time.minutes}:${time.seconds}`
}

What I learned from other people’s code: OK, so I see people have created that inner function to call 3 times, and did so very elegantly! Than they returned the function call interpolated with the : Truly concise and impressive! Hmm. So maybe I should spend more time refactoring my code. Although I do see that 31 people did that together!! Power of the pairing 🙂

I do see someone here using =~~. I’ve never seen this, and not sure how to look this one up since google is not really allowing me to search for it. Will ask some of my friends what they think!

Have a good one!

Day 5 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: www.codewars.com/r/w4mfhw

Time: 5 minutes

What I’ve learnt: I think this took me longer to read through all the instructions than to accomplish the kata! I could not figure out how to make them dummy components, so I just made them all react components.

This was a rather simple kata, with not much brainwork required.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
const React = require('react');

class Hello extends React.Component {
    render() {
        return <h1>Hello</h1>;    
    }
}

class World extends React.Component {
    render() {
        return <h2>World!</h2>;    
    }
}

class Greet extends React.Component {
    render() {
        return <div><Hello/><World/></div>;    
    }
}

 

What I learned from other people’s code:

I do like how other people created their dummy components by just creating a variable with the component name with a simple anonymous function returning the expected text.

This just shows me how React really is just using the ES6 class features in JS.

And onto the next 🙂

Day 4 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/begin-your-day-with-a-challenge-but-an-easy-one/javascript

Time: 40 minutes

What I’ve learnt: OK, so this was a 2 AM challenge! Implementing the simple one digit part, took about 6 minutes. I returned the default value of [0, 0] so that would not have to run through the rest of the code as I did on the past.

But it literally took me like 25 minutes to figure out what they wanted on the multiple digits. After about 10 minutes, I decided to press “Attempt” and not just keep looking at that 1 sample test, and then I was on the road to cracking the puzzle. It was enjoyable to have this mystery included in the code 🙂

I also recently realized I can add elements directly to an index of an array, so did that for the fun of it!

Here was my solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function oneTwoThree(n) {
    // justDoIt!!
    
    if (n === 0) return ['0', '0']
    
    const array = ["", ""]
    
    function addNums(condition, arrayI, num){
      for (let i = 0; i < condition; i++){
         array[arrayI] += num
       }
    }
    
    addNums(n, 1, 1)
     
    if (n % 9 === 0){
      addNums((parseInt(n)/9), 0, 9)
    } else {
      addNums(Math.floor(parseInt(n)/9), 0, 9)
      array[0] += parseInt(n) % 9
    }
    
    return array
}

 

What I learned from other people’s code:

I did not know there was a repeat method built into Javascript. I’m very excited to have seen that!

It was amazing how people used logic to avoid an iterator.

Array.prototype.fill() is also a super handy new method I learned from seeing other people’s solutions.

Now gotta start using it so I don’t lose it!

Copyright Loving to Code 2025
Tech Nerd theme designed by Siteturner