sashomaga

Count of all substrings

Mar 26th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function searchSubstring()
  2. {
  3.     var haystack = "We are living in an yellow submarine. We don't have anything else. Inside the submarine is very tight. So we are drinking all the day. We will move out of it in 5 days.";
  4.     var needle = document.getElementById("problem3").value;
  5.     //case insensitive
  6.     haystack = haystack.toLowerCase();
  7.     needle = needle.toLowerCase();
  8.     var count = 0;
  9.     var index = haystack.indexOf(needle);
  10.     if (index >= 0) {
  11.         count++;
  12.     }
  13.     while ((index = haystack.indexOf(needle, index+1)) && index != -1)
  14.     {
  15.         count++;
  16.     }
  17.     cons.innerHTML = count;
  18.  
  19. }
Advertisement
Add Comment
Please, Sign In to add comment