Task: Write a function that takes a number and returns an array of strings, each element being the number cut off at each digit. Examples: For 420, the function should return ["4", "42", "420"]; For 2017, the function should return ["2", "20", "201", "2017"]
I've written some code to solve this problem, but I'm looking for a more concise solution without using push method. It took me quite a while to figure out a declarative approach. Thank you.
function createArrayOfTiers(num) {
const arrT= num.toString().split("");
let z = [];
const result = arrT.reduce((acc, curr) => {
acc += curr;
z.push(acc);
return acc;
}, "");
return z;
}
Note: The input will always be an integer within the range [0, 1000000]