JSFiddle - React, Tailwind, and code Playground

by Andrey Zakharov

JavaScript

function countWordsInString(str, word) {

	var count = 0;
  var wordLength = word.length;
  var index = 0;
  var currentIndex = 0;
  while(true) {
  
  	currentIndex = str.substr(index + wordLength).indexOf(word);
		index += currentIndex + 1;
    
    if(currentIndex == -1) {
    	break;
     }
    count++;
  
  } 
  
    return count;

}

alert(countWordsInString("Бобры бобры добры ры", "ры"));