Number Split into individual digits in JavaScript


In the given problem statement we are presented with a digit and our task is to split it into individual digits with the help of Javascript. So we can convert the given number into a string and then we can use it in string manipulation techniques to get the desired result.

Understanding the Problem

In Javascript, it is very common to manipulate numbers at the individual digit level. So this is a common task to split the given number into its constituent digits. And extract it for further process or analysis.

Logic for the given Problem

To do this problem, we will use two different methods to split the number into its individual digits in Javascript. So we will solve this problem in two ways. In the first approach we will convert the number into a string representation and then with the usage of string manipulation method we will separate the digits. In the second method we will use mathematical operations to extract every digit from the number.

Algorithm - First Method

Step 1: Declare the number to which we have to convert into digits.

Step 2: Separate the numbers into a string and add it using the string manipulation methods.

Step 3: After converting the number into its digits we will console the digits variable to show the output.

Example

//Define the number to split into digits
let number = 54321;
let digits = number.toString().split('');
console.log(digits);

Output

[ '5', '4', '3', '2', '1' ]

Algorithm - Second Method

Step 1: Begin the program by declaring the number to be split into the digits and store it in 'number' variable.

Step 2: As we want to store the splitted digits in an array, create a blank array with the name 'digits'.

Step 3: Then enter a while loop which will continue processing the number until it reaches zero.

Step 4: In every step of the loop, using the modulus operator we will get the last digit of the supplied number while dividing by 10 to calculate the residual.

Step 5: We will update the number by dividing it with 10. This process will continue to run until the number becomes zero.

Step 6: After the loop is finished then we will store the digits in the digits array and show as a separate item in the correct order.

Example

//Define the number to split into digits
let num = 987456;
let digits = [];

while (num > 0) {
  digits.unshift(num % 10);
  num = Math.floor(num / 10);
}

console.log(digits);

Output

[ 9, 8, 7, 4, 5 ]

Complexity

The time complexity for the first method depends on conversion of the given number into a string and the second method depends as we have used modulus and division to get the digits. So the complexity for these two approaches is the same as O(log 10(num)). In this num is the number for which we have found the digits.

Conclusion

We have solved the given task in two ways in Javascript. Also the codes required some built in methods of Javascript, which makes code easy and short. Both the functions effectively find out the digits of the given number in different ways.

Updated on: 16-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements