I am trying to write a problem that outputs a similar pattern to this using recursion.
cascade(12345) //should print
12345
1234
123
12
1
12
123
1234
12345
I figured out how to do the descending part but I am stuck on how to ascend back up! This is what I have so far...
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 successfully outputs
'143'
'14'
'1'
How would I add the numbers back onto it one by one afterward?
Thank you for your time!
Aucun commentaire:
Enregistrer un commentaire