I've been stuck trying to create the function mentioned above for something I am working on
function expandedForm(num: number): number[] {
// ...
}
const k = expandedForm(8571);
console.log(k);
// [ 8000, 500, 70, 1 ]
Searching for this online only finds functions which return a string with + pluses between the numbers. Some help will be appreciated.
You can manipulate the input using the remainder operator:
function expandedForm(num: number): number[] {
const arr: number[] = [];
var x = num;
// Iterate until `x` is greater than zero
//
// For each iteration, keep track of the `i`:
// First iteration, i == 0, 10^i == 10^0 == 1
// Second iteration, i == 1, 10^i == 10^1 == 10
// Third iteration, i == 2, 10^i == 10^2 == 100
// This variable will let you know the current decimal place
for (var i = 0; x > 0; i++) {
// Get the last digit of `x` by using the remainder operator
const currentDigit = x % 10;
// Insert to the array using the previous algorithm
arr.push(Math.pow(10, i) * currentDigit);
// Remove the last digit of `x` using the floor of the division by 10
x = Math.floor(x / 10);
}
// Reverse the array
return arr.reverse();
}
const k = expandedForm(8571);
console.log(k);
Alternatively, you can insert to the first element of arr, without needing to reverse it in the end:
function expandedForm(num: number): number[] {
const arr: number[] = [];
var x = num;
for (var i = 0; x > 0; i++) {
const currentDigit = x % 10;
// Insert into the first element of `arr`
arr.splice(0, 0, Math.pow(10, i) * currentDigit);
x = Math.floor(x / 10);
}
// Just return the array
return arr;
}
const k = expandedForm(8571);
console.log(k);
Please, next time, include your own attempt and explain how it's failing.
But since there is already a working answer here, I'll simply post my versions, which are both different from the above.
One approach is to do this mathematically, using recursion, and passing values of 8571 to 857, then 85, and finally 8, adding the correct multiples of each power of ten to the running list. It could look like this:
const expandedForm = (n, p = 1, d = n % 10) =>
n < 10
? [n * p]
: expandedForm ((n - d) / 10, p * 10) .concat (d * p)
console .log (expandedForm (8571))
p here is the power of ten currently at play, and d is the final digit of the number. p starts at 1 and we multiply by 10 at every step.
A second approach is to convert n to a string, split it into digits and then map each one to the appropriate multiple of a power of ten:
const expandedForm = (n, [... ds] = String (n)) =>
ds .map ((d, i) => d * 10 ** (ds .length - 1 - i))
console .log (expandedForm (8571))
Of the two, I prefer the elegance of the first, more mathematical, one. But either works fine.
Note that both of these will leave zeros inside the output, so that, for instance, 8501 yields [8000, 500, 0, 1]. That may or may not be appropriate. If you would rather get [8000, 500, 1], then either of these is easy to fix:
const expandedForm = (n, p = 1, d = n % 10) =>
n < 10
? [n * p]
: expandedForm ((n - d) / 10, p * 10) .concat (d > 0 ? d * p : [])
or
const expandedForm = (n, [...ds] = String (n)) =>
ds .filter (d => d !== '0')
.map ((d, i) => d * 10 ** (ds.length - 1 - i))
These last two variants have slightly different behaviors regarding a final zero digit; we could fix either up to match the other if desired.