How to use mathematical operations In Javascript

In this approach we use a while loop to iterate over the digit of a number. In each iteration of the loop, we use the modulo operator (%) to get the last digit of the number and add it to the beginning of the array using unshift(). We then update the number by dividing it by 10 and taking the floor value using Math.floor(). This process repeats until the number becomes 0.

Example: This example shows the implementation of the above-explained approach.

JavaScript
function numberToArray(number) {
    let array = [];
    while (number > 0) {
        array.unshift(number % 10);
        number = Math.floor(number / 10);
    }
    return array;
}

let number = 12345;
let array = numberToArray(number);
console.log(array); 

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

How to convert a number into array in JavaScript ?

In this article, we have been given a number and the task is to convert the given number into an array using JavaScript.

These are the following methods by using these we can solve this problem:

Table of Content

  • Using Array.from() Method
  • Using map() Method
  • Using reduce() function
  • Using Lodash _.toArray() Method
  • Using mathematical operations
  • Using a for loop to iterate through the string representation

Similar Reads

Using Array.from() Method

The JavaScript Array from() method returns an Array object from any object with a length property or an iterable object.In this approach, Store a number in a variable.Use Array.from() method and in its first parameter enter the string type cast value.In the second parameter, we use a function i.e. myFunc, at every iteration the function will be called.The myFunc function will take a parameter that was returned by the iteration of Array.from() method. We typecast the number into a string so the type of parameter is a string, but we will typecast it into an integer and return it.The value returned by the Array.from() is our result....

Using map() Method

In this aproach, we will Store the integer value in a variable.Typecast the integer into a string.Using the split() method to make it an array of strings.Iterate over that array using the map() method.Using the map() method returns the array of strings into an array of Integers....

Using reduce() function

The reduce function is used to reduce the array into a single value and execute a provided function for each value of the array. Store the integer value in a variable.Typecast the integer into a string.Using the spread operator to form an array of characters.Iterate over that array using the reduce() method.Using the reduce() method convert the array of strings into an array of Integers....

Using Lodash _.toArray() Method

In this approach, we are using lodash library.by using this we can convert the given numbers into the array....

Using mathematical operations

In this approach we use a while loop to iterate over the digit of a number. In each iteration of the loop, we use the modulo operator (%) to get the last digit of the number and add it to the beginning of the array using unshift(). We then update the number by dividing it by 10 and taking the floor value using Math.floor(). This process repeats until the number becomes 0....

Using a for loop to iterate through the string representation

The for loop approach involves converting the number to a string, iterating through each character of the string, and converting each character back to a number. These numbers are then pushed into an array, resulting in an array of the number’s digits....