arrays - javascript,searching my name -


i making program in javascript searches name in text , logs frequency of same in console. first checking each letter of text , when matches name's first letter, use loop pushes letters array named hits.the letters string "text" pushed upto length of name using push(). after check whether array "hits" , string "myname" equal,and if equal increase count one. code not working , don't know why,i have thought on went in vain. please help.

var text="abhishek apolo bpple abhishek",myname="abhishek",hits=[];  var count=0;  for(i=0;i<text.length;i++)  {      if(text[i]===myname[0])      {          for(j=i;j<(i+myname.length);j++)          {              hits.push(text[j]);          }      }      if(myname==hits)      {          hits=[];          count=count+1;      }      hits=[];  }  if(count===0)      console.log("name not found!");  else      console.log(count);            

your code failing because comparing array string, give false, can string array string using join(), or better method use regular expression, this:

var text="abhishek apolo bpple abhishek",myname="abhishek",hits=[];  var count=0;  console.log((text.match(new regexp(myname, 'g'))).length);              


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -