DEV Community

loading...
Cover image for Solving 1 random codewars challenge in Typescript per day - Day 0

Solving 1 random codewars challenge in Typescript per day - Day 0

Iván Roldán Lusich
・2 min read

Today's challenge (direct link):

You are given two arrays a1 and a2 of strings. Each string is composed with letters from a to z. Let x be any string in the first array and y be any string in the second array.

Find max(abs(length(x) − length(y)))
Enter fullscreen mode Exit fullscreen mode

If a1 and/or a2 are empty return -1 in each language except in Haskell (F#) where you will return Nothing (None).

Example

a1 = ["hoqq", "bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"]
a2 = ["cccooommaaqqoxii", "gggqaffhhh", "tttoowwwmmww"]
mxdiflg(a1, a2) --> 13
Enter fullscreen mode Exit fullscreen mode

My solution

This is the original solution I came up with:

export class G964 {

    public static mxdiflg = (a1:string[], a2:string[]) => {
        if(a1.length === 0 || a2.length === 0) return -1;

        let a1Long = -1, a1Short = 900, a2Long = -1,a2Short = 900;

        a1.forEach((str) => {
          const strLen = str.length;
          if(strLen > a1Long){
            a1Long = strLen;
          }

          if(strLen < a1Short){
            a1Short = strLen;
          }
        })

        a2.forEach((str) => {
            const strLen = str.length;
            if(strLen > a2Long){
              a2Long = strLen;
            }

            if(strLen < a2Short){
              a2Short = strLen;
            }
          })

        const a1a2res = a1Long - a2Short;
        const a2a1res = a2Long - a1Short;

        if(a1a2res - a2a1res > a2a1res - a1a2res){
          return a1a2res
        } else {
          return a2a1res;
        }

    }
}
Enter fullscreen mode Exit fullscreen mode

As you can see, it's really verbose, but it's solving the problem. I'm going to refactor it to something better (I hope), and then I'll explain the code.

Refactor

export class G964 {

    public static mxdiflg = (a1:string[], a2:string[]) => {
        if(a1.length === 0 || a2.length === 0) return -1;

        const a1Long = Math.max(...a1.map(str => str.length));
        const a1Short = Math.min(...a1.map(str => str.length));
        const a2Long = Math.max(...a2.map(str => str.length));
        const a2Short = Math.min(...a2.map(str => str.length));

        const a1a2res = a1Long - a2Short;
        const a2a1res = a2Long - a1Short;

        if(a1a2res - a2a1res > a2a1res - a1a2res){
          return a1a2res
        } else {
          return a2a1res;
        }

    }
}
Enter fullscreen mode Exit fullscreen mode

That's my refactored code, it's much concise and works just as well. I could try and make it even shorter, but I think that would take away readability and that's not something I'm a fan of.

All in all, the solution to this Kata wasn't that hard, I calculated the longest word in each array doing

const a1Long = Math.max(...a1.map(str => str.length));
Enter fullscreen mode Exit fullscreen mode

which transforms the array of strings to an array containing the length of said strings, and then with Math.max() I take the maximum value of that array.

Pretty much the exact same process for calculating the minimum length, but this time using Math.min().

After that, I check which of the two maximum distances between the sets is bigger, and return it as a result.

And that is it! First day completed!.
I haven't done much on sites like codewars, but I really hope they get harder as time goes by, I love challenges!

See you tomorrow with another kata solved, Thanks for reading!.

Discussion (0)

Forem Open with the Forem app