Maximum call stack size error in running a simple recursive function












1















I'm pretty new to JavaScript programming but I had experience in so many other programming languages. The idea of recursive functions in JavaScript is a fresh subject for me and I didn't see anything similar in other languages that I worked with. So for the sake of practicing, I decided to write some of the programs that I already have written with "for loops".



One of these programs is a function that takes a string as its argument and reports how many B letters are in it. Using objective-oriented programming, I first declared a function that can find the number of any other characters within a string. The program is as follows,



function countChar(string, char) {
let counted = 0;
for (let index = 0; index < string.length; index++) {
if (string[index].toUpperCase() == char.toUpperCase()) {
counted += 1;
}
}
return counted;
}

function countBs(text) {
return countChar(text, 'B');
}

console.log(countBs('Baby'));
// output = 2


It works very well but now that I'm using recursive functions, I get the "Maximum call stack size" error. My program with recursive functions looks like this one,



function countChar(string, char) {
function cursor(i, counted) {
if (i == string.length) {
return counted;
} else if (string[i].toUpperCase() == char.toUpperCase()) {
return cursor(i++, counted++);
} else {
return cursor(i++, counted);
}
}
return cursor(0,0);
}

function countBs(text) {
return countChar(text, 'B');
}

console.log(countBs('Baby'));
// output must be 2 but I get 'Maximum call stack size' error instead :(


Can anybody offers a modification to this program in order to get the solution? Is it basically possible to write this program by using the recursive functions?










share|improve this question


















  • 1





    Consider renaming string and char to some not-so-system names.

    – Justinas
    Nov 14 '18 at 16:08
















1















I'm pretty new to JavaScript programming but I had experience in so many other programming languages. The idea of recursive functions in JavaScript is a fresh subject for me and I didn't see anything similar in other languages that I worked with. So for the sake of practicing, I decided to write some of the programs that I already have written with "for loops".



One of these programs is a function that takes a string as its argument and reports how many B letters are in it. Using objective-oriented programming, I first declared a function that can find the number of any other characters within a string. The program is as follows,



function countChar(string, char) {
let counted = 0;
for (let index = 0; index < string.length; index++) {
if (string[index].toUpperCase() == char.toUpperCase()) {
counted += 1;
}
}
return counted;
}

function countBs(text) {
return countChar(text, 'B');
}

console.log(countBs('Baby'));
// output = 2


It works very well but now that I'm using recursive functions, I get the "Maximum call stack size" error. My program with recursive functions looks like this one,



function countChar(string, char) {
function cursor(i, counted) {
if (i == string.length) {
return counted;
} else if (string[i].toUpperCase() == char.toUpperCase()) {
return cursor(i++, counted++);
} else {
return cursor(i++, counted);
}
}
return cursor(0,0);
}

function countBs(text) {
return countChar(text, 'B');
}

console.log(countBs('Baby'));
// output must be 2 but I get 'Maximum call stack size' error instead :(


Can anybody offers a modification to this program in order to get the solution? Is it basically possible to write this program by using the recursive functions?










share|improve this question


















  • 1





    Consider renaming string and char to some not-so-system names.

    – Justinas
    Nov 14 '18 at 16:08














1












1








1








I'm pretty new to JavaScript programming but I had experience in so many other programming languages. The idea of recursive functions in JavaScript is a fresh subject for me and I didn't see anything similar in other languages that I worked with. So for the sake of practicing, I decided to write some of the programs that I already have written with "for loops".



One of these programs is a function that takes a string as its argument and reports how many B letters are in it. Using objective-oriented programming, I first declared a function that can find the number of any other characters within a string. The program is as follows,



function countChar(string, char) {
let counted = 0;
for (let index = 0; index < string.length; index++) {
if (string[index].toUpperCase() == char.toUpperCase()) {
counted += 1;
}
}
return counted;
}

function countBs(text) {
return countChar(text, 'B');
}

console.log(countBs('Baby'));
// output = 2


It works very well but now that I'm using recursive functions, I get the "Maximum call stack size" error. My program with recursive functions looks like this one,



function countChar(string, char) {
function cursor(i, counted) {
if (i == string.length) {
return counted;
} else if (string[i].toUpperCase() == char.toUpperCase()) {
return cursor(i++, counted++);
} else {
return cursor(i++, counted);
}
}
return cursor(0,0);
}

function countBs(text) {
return countChar(text, 'B');
}

console.log(countBs('Baby'));
// output must be 2 but I get 'Maximum call stack size' error instead :(


Can anybody offers a modification to this program in order to get the solution? Is it basically possible to write this program by using the recursive functions?










share|improve this question














I'm pretty new to JavaScript programming but I had experience in so many other programming languages. The idea of recursive functions in JavaScript is a fresh subject for me and I didn't see anything similar in other languages that I worked with. So for the sake of practicing, I decided to write some of the programs that I already have written with "for loops".



One of these programs is a function that takes a string as its argument and reports how many B letters are in it. Using objective-oriented programming, I first declared a function that can find the number of any other characters within a string. The program is as follows,



function countChar(string, char) {
let counted = 0;
for (let index = 0; index < string.length; index++) {
if (string[index].toUpperCase() == char.toUpperCase()) {
counted += 1;
}
}
return counted;
}

function countBs(text) {
return countChar(text, 'B');
}

console.log(countBs('Baby'));
// output = 2


It works very well but now that I'm using recursive functions, I get the "Maximum call stack size" error. My program with recursive functions looks like this one,



function countChar(string, char) {
function cursor(i, counted) {
if (i == string.length) {
return counted;
} else if (string[i].toUpperCase() == char.toUpperCase()) {
return cursor(i++, counted++);
} else {
return cursor(i++, counted);
}
}
return cursor(0,0);
}

function countBs(text) {
return countChar(text, 'B');
}

console.log(countBs('Baby'));
// output must be 2 but I get 'Maximum call stack size' error instead :(


Can anybody offers a modification to this program in order to get the solution? Is it basically possible to write this program by using the recursive functions?







javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 16:05









Saeed AhadianSaeed Ahadian

1115




1115








  • 1





    Consider renaming string and char to some not-so-system names.

    – Justinas
    Nov 14 '18 at 16:08














  • 1





    Consider renaming string and char to some not-so-system names.

    – Justinas
    Nov 14 '18 at 16:08








1




1





Consider renaming string and char to some not-so-system names.

– Justinas
Nov 14 '18 at 16:08





Consider renaming string and char to some not-so-system names.

– Justinas
Nov 14 '18 at 16:08












1 Answer
1






active

oldest

votes


















4














because



 return cursor(i++, counted++);


has to be



 return cursor(i + 1, counted + 1);


(as you want to increase the value passed recursively, not the local variable i)





How i would do that:



 const countBs = (str, i = 0) => 
i >= str.length
? 0
: countBs(str, i + 1) + (str[i].toUpperCase() === "B");


Or if you plan to use it for very very long strings, allow for TCO:



 function countBs(str, i = 0, count = 0) {
if(i >= str.length) return count;
return countBs(str, i + 1, count + (str[i].toUpperCase === "B"));
}





share|improve this answer





















  • 1





    Could it be done return cursor(++i, counted)

    – George Jempty
    Nov 14 '18 at 16:11











  • @george yes, but I don't see any sense in increasing i ...

    – Jonas Wilms
    Nov 14 '18 at 16:12











  • @georg right, edited

    – Jonas Wilms
    Nov 14 '18 at 16:21











  • @JonasWilms So "++" operator just works locally? Is that why it works fine with index++ in for loop?

    – Saeed Ahadian
    Nov 14 '18 at 16:30













  • @saeed it evaluates to index, and then afterwards increases it.

    – Jonas Wilms
    Nov 14 '18 at 16:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304329%2fmaximum-call-stack-size-error-in-running-a-simple-recursive-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














because



 return cursor(i++, counted++);


has to be



 return cursor(i + 1, counted + 1);


(as you want to increase the value passed recursively, not the local variable i)





How i would do that:



 const countBs = (str, i = 0) => 
i >= str.length
? 0
: countBs(str, i + 1) + (str[i].toUpperCase() === "B");


Or if you plan to use it for very very long strings, allow for TCO:



 function countBs(str, i = 0, count = 0) {
if(i >= str.length) return count;
return countBs(str, i + 1, count + (str[i].toUpperCase === "B"));
}





share|improve this answer





















  • 1





    Could it be done return cursor(++i, counted)

    – George Jempty
    Nov 14 '18 at 16:11











  • @george yes, but I don't see any sense in increasing i ...

    – Jonas Wilms
    Nov 14 '18 at 16:12











  • @georg right, edited

    – Jonas Wilms
    Nov 14 '18 at 16:21











  • @JonasWilms So "++" operator just works locally? Is that why it works fine with index++ in for loop?

    – Saeed Ahadian
    Nov 14 '18 at 16:30













  • @saeed it evaluates to index, and then afterwards increases it.

    – Jonas Wilms
    Nov 14 '18 at 16:41
















4














because



 return cursor(i++, counted++);


has to be



 return cursor(i + 1, counted + 1);


(as you want to increase the value passed recursively, not the local variable i)





How i would do that:



 const countBs = (str, i = 0) => 
i >= str.length
? 0
: countBs(str, i + 1) + (str[i].toUpperCase() === "B");


Or if you plan to use it for very very long strings, allow for TCO:



 function countBs(str, i = 0, count = 0) {
if(i >= str.length) return count;
return countBs(str, i + 1, count + (str[i].toUpperCase === "B"));
}





share|improve this answer





















  • 1





    Could it be done return cursor(++i, counted)

    – George Jempty
    Nov 14 '18 at 16:11











  • @george yes, but I don't see any sense in increasing i ...

    – Jonas Wilms
    Nov 14 '18 at 16:12











  • @georg right, edited

    – Jonas Wilms
    Nov 14 '18 at 16:21











  • @JonasWilms So "++" operator just works locally? Is that why it works fine with index++ in for loop?

    – Saeed Ahadian
    Nov 14 '18 at 16:30













  • @saeed it evaluates to index, and then afterwards increases it.

    – Jonas Wilms
    Nov 14 '18 at 16:41














4












4








4







because



 return cursor(i++, counted++);


has to be



 return cursor(i + 1, counted + 1);


(as you want to increase the value passed recursively, not the local variable i)





How i would do that:



 const countBs = (str, i = 0) => 
i >= str.length
? 0
: countBs(str, i + 1) + (str[i].toUpperCase() === "B");


Or if you plan to use it for very very long strings, allow for TCO:



 function countBs(str, i = 0, count = 0) {
if(i >= str.length) return count;
return countBs(str, i + 1, count + (str[i].toUpperCase === "B"));
}





share|improve this answer















because



 return cursor(i++, counted++);


has to be



 return cursor(i + 1, counted + 1);


(as you want to increase the value passed recursively, not the local variable i)





How i would do that:



 const countBs = (str, i = 0) => 
i >= str.length
? 0
: countBs(str, i + 1) + (str[i].toUpperCase() === "B");


Or if you plan to use it for very very long strings, allow for TCO:



 function countBs(str, i = 0, count = 0) {
if(i >= str.length) return count;
return countBs(str, i + 1, count + (str[i].toUpperCase === "B"));
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 16:21

























answered Nov 14 '18 at 16:07









Jonas WilmsJonas Wilms

58.1k43051




58.1k43051








  • 1





    Could it be done return cursor(++i, counted)

    – George Jempty
    Nov 14 '18 at 16:11











  • @george yes, but I don't see any sense in increasing i ...

    – Jonas Wilms
    Nov 14 '18 at 16:12











  • @georg right, edited

    – Jonas Wilms
    Nov 14 '18 at 16:21











  • @JonasWilms So "++" operator just works locally? Is that why it works fine with index++ in for loop?

    – Saeed Ahadian
    Nov 14 '18 at 16:30













  • @saeed it evaluates to index, and then afterwards increases it.

    – Jonas Wilms
    Nov 14 '18 at 16:41














  • 1





    Could it be done return cursor(++i, counted)

    – George Jempty
    Nov 14 '18 at 16:11











  • @george yes, but I don't see any sense in increasing i ...

    – Jonas Wilms
    Nov 14 '18 at 16:12











  • @georg right, edited

    – Jonas Wilms
    Nov 14 '18 at 16:21











  • @JonasWilms So "++" operator just works locally? Is that why it works fine with index++ in for loop?

    – Saeed Ahadian
    Nov 14 '18 at 16:30













  • @saeed it evaluates to index, and then afterwards increases it.

    – Jonas Wilms
    Nov 14 '18 at 16:41








1




1





Could it be done return cursor(++i, counted)

– George Jempty
Nov 14 '18 at 16:11





Could it be done return cursor(++i, counted)

– George Jempty
Nov 14 '18 at 16:11













@george yes, but I don't see any sense in increasing i ...

– Jonas Wilms
Nov 14 '18 at 16:12





@george yes, but I don't see any sense in increasing i ...

– Jonas Wilms
Nov 14 '18 at 16:12













@georg right, edited

– Jonas Wilms
Nov 14 '18 at 16:21





@georg right, edited

– Jonas Wilms
Nov 14 '18 at 16:21













@JonasWilms So "++" operator just works locally? Is that why it works fine with index++ in for loop?

– Saeed Ahadian
Nov 14 '18 at 16:30







@JonasWilms So "++" operator just works locally? Is that why it works fine with index++ in for loop?

– Saeed Ahadian
Nov 14 '18 at 16:30















@saeed it evaluates to index, and then afterwards increases it.

– Jonas Wilms
Nov 14 '18 at 16:41





@saeed it evaluates to index, and then afterwards increases it.

– Jonas Wilms
Nov 14 '18 at 16:41




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304329%2fmaximum-call-stack-size-error-in-running-a-simple-recursive-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine