In this function i am trying to calculate the number of ways, a number can be decoded. 1 ought to be decoded as a, 3 as c, 26 as z . The function calculates the right count, but does only return undefined. I think it should cancel the recursive calls at the right time, and I reach the "escape"-block, but the number is not returned as it should. Can anyone point me towards the reason, why this is happening?
function numWaysDecodable(msg) {
msg = msg.toString().split("");
function helper(msg, past = 99, count = 1) {
if (msg.length === 0) {
console.log("wtf count is:"+count);
return count;
}
let head = msg.shift();
if (head < 7 && past < 3) {
count++
}
//the below return statement was missing
return helper(msg, head, count);
}
return helper(msg);
}
console.log(numWaysDecodable(123));
- 12 1 2;
- 1 2 12;
- 12 12;
- 1 2 1 2;
- 1 21 2;
I think the code misses to count nr.3,12 12; I am unsure how to fix that as of yet. Some more thinking to do