Question

function occurrence(string,substring) {
    var counter = 0;
    var sub = substring.toLowerCase();
    var str = string.toLowerCase(); 
    var array = []
    var ans;

    for (var i = 0; i < str.length; i++) {
        if (str.indexOf(sub) != -1) {
            array[counter] = str.indexOf(sub);
            counter += 1;
            ans = array.length
            alert(array); //tester to see inside array
        } else {
            ans = 0;
        }
    }
    return ans
}

alert(occurrence("Party arty art","art")) //tester to see function in action

In the tester shown above, it should print out 3. But with my code, it prints out the length of the string. I'm trying to store the index of where "art" shows up in the string in an array so that I can print out the array to give the number of occurrences. However when I alerted the array inside the loop, it just prints out "1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1".

Note: I'm trying to write this code with as few built in functions as possible. I'm new to javascript so some guidance will be much appreciated.

Also I'm trying to write this without .match()

Was it helpful?

Solution

What about something like this?

function occurrence(string, substring) {
    var counter = 0;
    var sub = substring.toLowerCase();
    var str = string.toLowerCase(); 
    var array = [];
    var index = -1;

    do {
        index = str.indexOf(sub, index + 1);
        if (index != -1) {
            array[counter++] = index;
            i = index;
        }
    } while (index != -1);

    return counter; // or return array; if you want the indexes
}

OTHER TIPS

Have you considered doing a regular expression match? That would probably be the best thing in this case.

Here you go: (PS, you might want to avoid using built in names like "string" and "integer" when naming variables)

you need to create a function to escape any regex characters from your string:...then use that function.

function occurrence(str, substr) {
  var escaped = escapeFn(substr)
  var regex = new RegExp(escaped,"g")

  return str.match(regex).length

}

EDIT: sorry, i must have missed at the bottom where you said you didn't want to use match.

Might want to use match():

Search a string for "ain":

var str = "The rain in SPAIN stays mainly in the plain"; 
var res = str.match(/ain/g);

The result of res will be an array with the values:

ain,ain,ain

So for your example, something like this might work:

function occurrence(string, substring) {
    var counter = 0;
    var sub = new RegExp(escapeRegexStringCharactersFunction(substring.toLowerCase()),"g");
    var str = string.toLowerCase(); 
    var array = []

    array = str.match(sub);
    counter = array.length;

    return counter;
}

Even simpler:

function occurrence(string, substring) { 
    var array = []
    var sub = new RegExp(escapeRegexStringCharactersFunction(substring),"gi");
    array = string.match(sub);
    return array.length;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top