Mastering the Technique to Square Each Digit in JavaScript
Written on
Chapter 1: Introduction to the Problem
The programming community is buzzing with activity, and I've chosen to step back from social media. It's possible I've missed out on some happenings, such as the absence of a DevAdvent event this year, which I found valuable last time. My schedule is tighter now, so I've decided to embark on a challenge: daily solving a random problem from CodeWars. Let's dive into today’s task.
Section 1.1: The Challenge
Today's coding challenge is straightforward: Square Every Digit. The task is to take a number and return a new number where each digit is squared. For instance, for the number 9119, the output should be 811181, while squaring 0 results in 0.
Subsection 1.1.1: Proposed Solution
The solution I suggest is implemented in TypeScript as follows:
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);
}
}
In JavaScript, the equivalent solution would be:
function squareDigits(num) {
const char = num.toString();
const result = [...char].map((c) => parseInt(c) ** 2).join("");
return +result;
}
Section 1.2: Breaking Down the Solution
To clarify the solution, I will dissect the problem into manageable parts. Initially, I convert the number into a string format.
const char: string = num.toString();
const char = num.toString();
This conversion allows me to isolate each individual digit. I can achieve this via the toString() method or by concatenating an empty string to the number.
const char: string = "" + num;
const char = "" + num;
While the problem primarily concerns positive integers, it’s worthwhile to consider negative values as well. To handle negative numbers, I can apply the Math.abs() function to convert them to positive.
const char: string = Math.abs(num).toString();
const char = Math.abs(num).toString();
After obtaining the string representation, I can transform it into an array, allowing me to utilize the Array.map() method.
Next, I convert the string into an array using the spread operator:
const array: Array = [...char];
const array = [...char];
To square a number, I can use the exponentiation operator:
const result: number = 2 ** 3;
const result = 2 ** 3;
To convert a string back to a number, the parseInt() method comes in handy:
const result: number = parseInt("123");
const result = parseInt("123");
Lastly, to join an array into a string, I can use the Array.join() method. The final result is constructed as follows:
const result: string = [...char].map((c) => parseInt(c) ** 2).join("");
const result = [...char].map((c) => parseInt(c) ** 2).join("");
The final step is to return the computed result as a number:
return parseInt(result);
Chapter 2: Exploring Alternative Solutions
The first video titled "Codewars 7 kyu Square Every Digit Javascript" provides a comprehensive look into different approaches for squaring digits.
The second video, "Codewars 7 kyu Square Every Digit JavaScript," dives deeper into strategies for solving this challenge, including the use of regular expressions and the String.replace() method.
Thank you for reading! Stay tuned for more insightful articles. Don't forget to subscribe to my Medium newsletter for updates. For additional content, follow me on various platforms like Twitter, LinkedIn, YouTube, and Discord. Interested in Growth Hacking? Explore Circuit for more resources.