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
ScriptOP3y 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
ScriptOP3y 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
ScriptOP3y 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
ScriptOP3y 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
ScriptOP3y 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
ScriptOP3y 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?
Script
ScriptOP3y ago
yes b is an integer
S3BAS
S3BAS3y ago
What does the length of 0 return 0.length
Script
ScriptOP3y ago
KomodoHype
S3BAS
S3BAS3y ago
And 3.length? I’m on phone so I can’t verify myself
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
S3BAS
S3BAS3y ago
Are you perhaps assuming that you can call length on a number without actually trying it out?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
S3BAS
S3BAS3y ago
Yeah
Script
ScriptOP3y ago
same thing invalid
S3BAS
S3BAS3y ago
So why are you calling length on a number 😛 Turn it to a string first
Script
ScriptOP3y ago
shocked my bad
S3BAS
S3BAS3y 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
Script
ScriptOP3y 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
S3BAS
S3BAS3y ago
Yeah you are indeed Since it will evaluate to a number, but that number needs to be returned to the caller
reactibot
reactibot3y ago
This question has an answer! Thank you for helping 😄 If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/938205834796744794 This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/938205834796744794