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);
}
S
ScriptyChris807d ago
because you don't return recall(b) call?
G
ghardin137807d ago
wow you could probably stand to name your variables a bit better 🙂
S
Script807d 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
S
ScriptyChris807d ago
i meant this
S
Script807d ago
😂 I was just rushing and giving it similar var names I tried that but still got undefined
S
ScriptyChris807d ago
is b.length === 1condition ever met?
S
Script807d ago
yes
S
ScriptyChris807d ago
so recall at that case should return non-undefined b variable, because it surely is array/string with length property equal to 1
S
Script807d ago
I'll look at my code yy
S
ScriptyChris807d 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?
S
S3BAS807d ago
Return the last line return recall(b) @Script
S
Script807d 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
S
S3BAS807d ago
Yeah then your base condition is never being met
S
Script807d ago
It is actually b becomes 3 that's length of 1
S
S3BAS807d 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?
S
Script807d ago
yes b is an integer
S
S3BAS807d ago
What does the length of 0 return 0.length
S
Script807d ago
KomodoHype
S
S3BAS807d ago
And 3.length? I’m on phone so I can’t verify myself
UU
Unknown User807d ago
S
S3BAS807d ago
Are you perhaps assuming that you can call length on a number without actually trying it out?
UU
Unknown User807d ago
S
S3BAS807d ago
Yeah
S
Script807d ago
same thing invalid
S
S3BAS807d ago
So why are you calling length on a number 😛 Turn it to a string first
S
Script807d ago
shocked my bad
S
S3BAS807d ago
Don’t just assume code will work like you expect it to, you need to test and confirm even the smallest things heh Yeah all good
S
Script807d ago
ayee still gettin undefined coolcry
function superDigit(n, k) {
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.toString().length === 1) {
return b;
} else {
recall(b);
}
}
return recall(b);
}
function superDigit(n, k) {
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.toString().length === 1) {
return b;
} else {
recall(b);
}
}
return recall(b);
}
wait am I supposed to return recall(b) in recall()? Because I did now and it worked
S
S3BAS807d ago
Yeah you are indeed Since it will evaluate to a number, but that number needs to be returned to the caller
UU
Unknown User806d ago