Advertisement
DNNdrago

12. Substring Count

Jul 24th, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. function countSubstringOccur(value) {
  2. var keyword = value[0];
  3. var text = value[1];
  4.  
  5. var index = 0,
  6. count = 0;
  7.  
  8. while(true) {
  9. var nextIndex = value.indexOf(keyword, index);
  10. if (nextIndex !== -1) {
  11. index = index + 1;
  12. count += 1;
  13. }
  14. else {
  15. break;
  16. }
  17. }
  18.  
  19. return count;
  20. }
  21.  
  22. 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