Advertisement
DJ_Zoning

Substring Count

Jul 18th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function countSubstringOccur(array) {
  2.  
  3.  
  4.     var keywordLength = array[0].length;
  5.     var textLength = array[1].length;
  6.     var keyword = array[0];
  7.     var text = array[1];
  8.     var firstSymbol = keyword[0];
  9.     var counter = 0;
  10.  
  11.     for (var i = 0; i < textLength; i++) {
  12.  
  13.         if (text[i] === firstSymbol) {
  14.             var count = 0;
  15.             for (var j = i; j < keywordLength; j++) {
  16.  
  17.                 if (keyword[j - i] === text[j]) {
  18.                     count++;
  19.                     if (count === keywordLength) {
  20.                         counter++;
  21.                     }
  22.                 }
  23.  
  24.             }
  25.  
  26.         }
  27.  
  28.     }
  29.     return counter;
  30. }
  31. console.log(countSubstringOccur(["in", "We are living in a 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."]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement