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!

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