1function count(str, find) {
2 return (str.split(find)).length - 1;
3}
4
5count("Good", "o"); // 2
1var temp = "This is a string.";
2var count = (temp.match(/is/g) || []).length;
3console.log(count);
4
5Output: 2
6
7Explaination : The g in the regular expression (short for global) says to search the whole string rather than just find the first occurrence. This matches 'is' twice.
1function countOccurences(string, word) {
2 return string.split(word).length - 1;
3}
4var text="We went down to the stall, then down to the river.";
5var count=countOccurences(text,"down"); // 2
1function count (string) {
2 var count = {};
3 string.split('').forEach(function(s) {
4 count[s] ? count[s]++ : count[s] = 1;
5 });
6 return count;
7}
1 const recorrences = ['a', 'b', 'c', 'a', 'b','a']
2 .map(i => !!~i.indexOf('a'))
3 .filter(i => i)
4 .length;
5console.log(`recorrences ${recorrences}`)
6//recorrences 3
1 function countInstancesOf(letter, sentence) {
2 var count = 0;
3
4 for (var i = 0; i < sentence.length; i++) {
5 if (sentence.charAt(i) === letter) {
6 count += 1;
7 }
8 }
9 return count;
10}