I am attempting to create a recursive function that can produce a pattern similar to the example below.
cascade(12345) //should print
12345
1234
123
12
1
12
123
1234
12345
While I have managed to achieve the descending part, I am now facing difficulty in ascending back up. Here is my current code:
function cascade(number) {
let strNum = number.toString()
let numLength = strNum.length;
let lengthTracker = numLength
let hasHit1 = false;
console.log(strNum)
if (lengthTracker > 1 && hasHit1 === false) {
strNum = strNum.substring(0, strNum.length - 1);
lengthTracker--;
return cascade(strNum)
} else {
return strNum;
}
}
cascade(143)
This code successfully outputs:
'143'
'14'
'1'
Is there a way to incrementally add the numbers back onto the pattern afterwards?
Thank you for your assistance!