2

I'm trying to write a recursive program to count the number of times a substring ("cat") appears in a string ("catdogcowcat"). Not sure what I'm doing wrong, but I keep getting the following error:

TypeError: Cannot read property 'length' of undefined

Here's my code:

function strCount (str, sub) {
    var subLen = sub.length;
    var strLen = str.length;
    if (strLen < subLen) {
        return 0;
    } else if (str.slice(0, subLen) === sub) {
        return 1 + strCount(str.substring(subLen));
    } else return strCount(str.substring(1));
}

I think it's breaking when I try to get the length of the substring on this line, but that's just my guess based on my infantile understanding of devtools debugging:

return 1 + strCount(str.substring(subLen));

Thanks!

4 Answers 4

4

Your strCount function takes 2 arguments, so make sure you pass sub when you call it recursively:

function strCount (str, sub) {
    var subLen = sub.length;
    var strLen = str.length;
    if (strLen < subLen) {
        return 0;
    } else if (str.slice(0, subLen) === sub) {
        return 1 + strCount(str.substring(subLen), sub);
    } else return strCount(str.substring(1), sub);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oops, shouldn't have missed that. Thanks!
0

You could use regular expression, with match instead of having to do a recursive function

function strCount(needle,haystack){
   var r = new RegExp(needle,"g");
   var r2 = haystack.match(r);
   return (r2?r2.length:0);
}

console.log( strCount("cat","catdowcowcat") );

Comments

0

you need to pass value for sub ..

 function strCount(str, sub) {                
            var subLen = sub.length;
            var strLen = str.length;
            if (strLen < subLen) {
                return 0;
            } else if (str.slice(0, subLen) === sub) {
                return 1 + strCount(str.substring(subLen),sub);
            } else
                return strCount(str.substring(1),sub);
        }

you can call the method

 $(document).ready(function(){
               alert(strCount("catdogcowcat","cat"));
            });

It will return the result as 2. Fiddle http://jsfiddle.net/deepaksuresh3003/RDmby/

Comments

0

you are calling your own method strCount(str.substring(subLen)) without providing second parameter and hence the function is taking 'sub' parameter as undefined.

Update your code as follows and it will start working:

 function strCount(str, sub) {
        var subLen = sub.length;
        var strLen = str.length;
        if (strLen < subLen) {
            return 0;
        } else if (str.slice(0, subLen) === sub) {
            return 1 + strCount(str.substring(subLen), sub);
        } else return strCount(str.substring(1),sub);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.