top of page

Program to find a word in JavaScript

Writer's picture: The Tech PlatformThe Tech Platform

This article will show you the steps to find a word from a given string in JavaScript using search_word



Code:

function search_word(text, word){
    
    var x = 0, y=0;
   
    for (i=0;i< text.length;i++)
        {
        if(text[i] == word[0])
            {
            for(j=i;j< i+word.length;j++)
               {
                if(text[j]==word[j-i])
                  {
                    y++;
                  }
                if (y==word.length){
                    x++;
                }
            }
            y=0;
        }
    }
   return "'"+word+"' was found "+x+" times.";
}
console.log('String - Technology is best when it brings people together');
console.log(search_word('Technology is best when it brings people together', 'Technology'));

Output:




The Tech Platform

1 comment

Recent Posts

See All

1 comentario


Irene Perez
Irene Perez
02 dic 2022

We are among the oldest.

Universal essay writer service has been here for years on end. This has left us with great years of experience in writing comparison essays not to mention of the dexterity that we possess on how to write a comparative essay.

Me gusta
bottom of page