Script
Script3y ago

Script – 22-55 Feb 1

I'm trying to run the function till b.length === 1 and then return b What am I doing wrong? it returns undefined
function superDigit(n, k) {
if (k === 1) return n;

n = n.toString();
let b = 0;
for (let i = 0; i < k; i++) {
n = n.concat(n);
}
for (let i in n) {
b += parseInt(n[i]);
}

function recall(bb) {
bb = bb.toString();
bbb = 0;
for (let i in bb) {
bbb += parseInt(bb[i]);
}
b = bbb;
if (b.length === 1) {
return b;
}
}

recall(b);
}
function superDigit(n, k) {
if (k === 1) return n;

n = n.toString();
let b = 0;
for (let i = 0; i < k; i++) {
n = n.concat(n);
}
for (let i in n) {
b += parseInt(n[i]);
}

function recall(bb) {
bb = bb.toString();
bbb = 0;
for (let i in bb) {
bbb += parseInt(bb[i]);
}
b = bbb;
if (b.length === 1) {
return b;
}
}

recall(b);
}
30 Replies
ScriptyChris
ScriptyChris3y ago
because you don't return recall(b) call?
ghardin137
ghardin1373y ago
wow you could probably stand to name your variables a bit better 🙂
Script
Script3y ago
function recall(bb) {
bb = bb.toString();
bbb = 0;
for (let i in bb) {
bbb += parseInt(bb[i]);
}
b = bbb;
console.log("2", b);
return b;
}
if (b.length === 1) {
return b;
} else return recall(b);
function recall(bb) {
bb = bb.toString();
bbb = 0;
for (let i in bb) {
bbb += parseInt(bb[i]);
}
b = bbb;
console.log("2", b);
return b;
}
if (b.length === 1) {
return b;
} else return recall(b);
Like thistbh
ScriptyChris
ScriptyChris3y ago
i meant this
Script
Script3y ago
😂 I was just rushing and giving it similar var names I tried that but still got undefined
ScriptyChris
ScriptyChris3y ago
is b.length === 1condition ever met?
Script
Script3y ago
yes
ScriptyChris
ScriptyChris3y ago
so recall at that case should return non-undefined b variable, because it surely is array/string with length property equal to 1
Script
Script3y ago
I'll look at my code yy
ScriptyChris
ScriptyChris3y ago
to be clear, you mean that superDigit returns undefined or recall? and are you sure that recursion ends sometime, not throws call stack exceeded error?
S3BAS
S3BAS3y ago
Return the last line return recall(b) @Script
Script
Script3y ago
Okay so I am finally exceeding the call stack
function superDigit(n, k) {
// if (k === 1) return n;

n = n.toString();
let n2 = n;
let b = 0;
for (let i = 0; i < k - 1; i++) {
n = n.concat(n2);
}
for (let i in n) {
b += parseInt(n[i]);
}

function recall(newVal) {
newVal = newVal.toString();
point0 = 0;
for (let i in newVal) {
point0 += parseInt(newVal[i]);
}
b = point0;
if (b.length == 1) {
return b;
} else {
recall(b);
}
}

return recall(b);
}
function superDigit(n, k) {
// if (k === 1) return n;

n = n.toString();
let n2 = n;
let b = 0;
for (let i = 0; i < k - 1; i++) {
n = n.concat(n2);
}
for (let i in n) {
b += parseInt(n[i]);
}

function recall(newVal) {
newVal = newVal.toString();
point0 = 0;
for (let i in newVal) {
point0 += parseInt(newVal[i]);
}
b = point0;
if (b.length == 1) {
return b;
} else {
recall(b);
}
}

return recall(b);
}
How do I manage this
S3BAS
S3BAS3y ago
Yeah then your base condition is never being met
Script
Script3y ago
It is actually b becomes 3 that's length of 1
S3BAS
S3BAS3y ago
If you would properly hit the base condition you wouldn’t exceed the call stack Are you calling length on a number? Does that give you the amount of digits?