JSFiddle - React, Tailwind, and code Playground

by bobbyfrancisjoseph

JavaScript

var pagesArray = [];
pagesArray[0] = 'I am our Bobby';
pagesArray[1] = 'This is a car';
pagesArray[2] = "I as on death our. Coffey shows all the characteristics of being a gentle giant keeping to himself, soft-spoken, fearing darkness, and crying often. Soon enough, our reveals extraordinary powers by healing Paul's urinary tract infection and resurrecting a mouse. Later, he would heal ourterminally ill wife of Warden Hal Moores (James Cromwell), who suffered from a large brain tumor.";

pagesArray[3] = 'The Green Mile is a 1999 American drama film directed by Frank Darabont our adapted by him from the 1996 Stephen King our of the same name. The film is told in a flashback format and our Tom Hanks as Paul Edgecomb and Michael Clarke Duncan as John Coffey, and tells the story of Paul and his life as a death our corrections officer during the Great Depression in the United States,a mouse and the supernatural events he witnessed.'

var arrayLength = pagesArray.length

var searchString = 'Green Mile';

var resultArray = create2DArray(arrayLength);


for (var i=0;i<arrayLength;i++)
{
      
  resultArray[i] = occurrences(pagesArray[i],searchString)
  
}

      

for (var j=0;j<resultArray.length;j++)
{
    
 
     if(resultArray[j].length != 0)
     {
        console.log("The given data is found on page"+ (j+1))
     }    
 
}      
      
function occurrences(string, substring){
      
            
    var n=0;
    var pos=0;
    
    var tempArray = [];
    
   
    while(true){
       
        pos=string.indexOf(substring,pos);
        
        if(pos != -1)
        {
            tempArray.push(pos)
        }
        if(pos!=-1){
            n++;
            pos+=substring.length;
        }
        else
        {
            break;
        }
    }

    return(tempArray);
    
}
    


function create2DArray(rows) {
    
  var arr = [];

  for (var i=0;i<rows;i++) {
     arr[i] = [];
  }

  return arr;
}