Good day, so I am trying to solve this one exercise. And try both methods of directly console.log a string.indexOf(subString) and using a function to count the occurences of a specific character/word on a string, which then returns both different values. Why is that?
Here is the code:
const longText = This is a very very long text. Do you understand how very long is this? Good for yah!
const word = "very"
console.log(longText.indexOf(word)); function
checkWord(longText,position) {
var n = 0;
var position= 0;
while(true) {
position= longText.indexOf(word, position)
if (position != -1) {
n++;
position += word.length;
} else {
break; } } return n; }
console.log(checkWord(longText, word));
Expected output:
10
3
Thank you in advance for those who will respond.