I keep getting a result of 0 whenever I type in a search term. I’m trying to find the number of times that a pattern occurs in string. So for example searching for at would return 3. Any advice on where I’m going wrng?
string =
[
"cat math path"
]
var pattern = prompt('Please enter a search term:');
function check(string,pattern)
{
if(pattern)
{
if(pattern.indexOf(string) == -1)
{
return 0;
}
return count(pattern.substring(pattern.indexOf(string)+string.length), string)+1;
}
else
{
return("Nothing entered!");
}
}
alert(check(string,pattern));
should be