The Twitter world is in turmoil. I’ve decided to abandon it. I am certainly missing something. For example I don’t know if anyone is organizing a DevAdvent like last year. It was a good experience last year and I would like to repeat it. But I don’t have all the time I had last year. So I decided to do something spurious: every day I will choose a random problem from those proposed by CodeWars and try to solve it. Let’s see how it goes.

The problem

Today’s problem is Square Every Digit. The problem is very simple: given a number, return a number obtained by squaring each digit of the number. For example, if the number is 9119, the result will be 811181. If the number is 0, the result will be 0.

The solution

mathematical equations in hoodie, chalkboard, portrait, vaporwave, synthwave, neon, vector graphics, cinematic, volumetric lighting, f 8 aperture, cinematic eastman 5 3 8 4 film, photorealistic

The solution I propose (in TypeScript) is this:

export class Kata {
  static squareDigits(num: number): number {
    const char: string = num.toString();
    const result: string = [...char].map((c) => parseInt(c) ** 2).join("");
    return parseInt(result);
  }
}

I can also write the solution in JavaScript:

function squareDigits(num) {
  const char = num.toString();
  const result = [...char].map((c) => parseInt(c) ** 2).join("");
  return +result;
}

Explanation

I can break the problem into several pieces in order to simplify the various steps.

First I convert the number to a string.

// ts
const char: string = num.toString();

// js
const char = num.toString();

Why am I doing this? Because this allows me to extract every single digit of the number. I can use the toString() method, or concatenate the empty string with the number. Both methods work the same way.

// ts
const char: string = "" + num;

// js
const char = "" + num;

The problem and the tests are about positive numbers, but it’s also interesting to consider negative numbers. I can resolve the issue in various ways. I can use Math.abs() method to convert number to positive.

// ts
const char: string = Math.abs(num).toString();
const char: string = "" + Math.abs(num);

// js
const char = Math.abs(num).toString();
const char = "" + Math.abs(num);

After finding the string I can convert it to an array so that I can use the Array.map() method.

Then I transform a string into an array using the spread operator:

// ts
const array: Array<string> = [...char];

// js
const array = [...char];

To make the power of a number I can use the Exponentiation operator (**)

// ts
const result: number = 2 ** 3;

// js
const result = 2 ** 3;

To convert a string to a number I can use the parseInt() method.

// ts
const result: number = parseInt("123");

// js
const result = parseInt("123");

Instead to convert an array into a string I use the Array.join() method.

Putting this together I get

// ts
const result: string = [...char].map((c) => parseInt(c) ** 2).join("");

// js
const result = [...char].map((c) => parseInt(c) ** 2).join("");

I just have to return the result as a number:

return parseInt(result);

Other solutions

Obviously it is not the only possible solution. For example, I can use the Array.reduce() method to get the result.

// ts
const result: number = +[...char].reduce((acc, cur) => acc + +cur * +cur, "");
return result;

That is to say:

// ts
export class Kata {
  static squareDigits(num: number): number {
    return +[...("" + Math.abs(num))].reduce(
      (acc, cur) => acc + +cur * +cur,
      ""
    );
  }
}

// js
function squareDigits(num) {
  return +[...("" + Math.abs(num))].reduce((acc, cur) => acc + +cur * +cur, "");
}

Another alternative is to use regular expressions and the String.replace() method.

// ts
export class Kata {
  static squareDigits(num: number): number {
    return +("" + Math.abs(num)).replace(/\d/g, (x) => (+x * +x).toString());
  }
}

// js
function squareDigits(num) {
  return +("" + Math.abs(num)).replace(/\d/g, (x) => (+x * +x).toString());
}