3

I am making a program in javascript which searches my name in a text and logs the frequency of the same in the console. I am first checking each letter of the text and when it matches with my name's first letter, I use another for loop which pushes letters into an array named hits.The letters from string "text" are pushed upto the length of my name using push(). After this I check whether the array "hits" and string "myName" are equal,and if they are equal I increase the count by one. But my code is not working and I don't know why,I have thought on it very much but all 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);
    
    
ashwini abhishek
  • 206
  • 5
  • 12
  • 1
    What about regexp ? – Pierre Granger Jul 26 '17 at 10:23
  • `if(myName==hits)` never passes, since you're comparing a string to an array. – Teemu Jul 26 '17 at 10:24
  • is there a reason you are doing it from scratch instead of using search functions like `indexOf`? – Kaddath Jul 26 '17 at 10:25
  • Yes I am comparing string to an array but I am using "==" instead of "===" – ashwini abhishek Jul 26 '17 at 10:27
  • Umh... the array is converted by using its `toString()` method, it returns "a,b,h,i,s,h,e,k". – Teemu Jul 26 '17 at 10:34
  • I have read that "==" doesn't compare data types. – ashwini abhishek Jul 26 '17 at 10:36
  • That is incorrect, it does compare data types, and if they are different, it tries to convert the operands to the same type by some very complex rules. `===` also compares data types, but it returns `false` immediately, if the types are different. Don't use `==`, if you're not absolutely sure how the types are converted. – Teemu Jul 26 '17 at 10:39
  • 1
    Thanks Teemu,can you suggest some links where I can read about the rules which you are talking about. Thanks for the help. – ashwini abhishek Jul 26 '17 at 10:45
  • [A good SO post about == vs. ===](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons), and [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators). – Teemu Jul 26 '17 at 10:50
  • Why you need such long code if you can do it in easy way? Your requirements are searching string/value inside another with knowing about number of occurrence. right? If yes then please see another stackoverflow post: https://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string Hopefully next time good google search can give you this kind of solution. Let us know if you really have any issue? Happy coding ! – Star Jul 26 '17 at 10:31
  • Why you need such long code if you can do it in easy way? Your requirements are searching string/value inside another with knowing about number of occurrence. right? If yes then please see another stackoverflow post: How to count string occurrence in string? Hopefully next time good google search can give you this kind of solution. Let us know if you really have any issue? Happy coding ! – Star Aug 21 '17 at 07:35

7 Answers7

4

your code is failing because you are comparing array to string, which would give you false, you can get string from array string using join(), or a better method would be to use regular expression, like this:

var text="abhishek apolo bpple abhishek",myName="abhishek",hits=[];
var count=0;
console.log((text.match(new RegExp(myName, 'g'))).length);        
    
Dij
  • 9,761
  • 4
  • 18
  • 35
1

You're comparing an array to a string. Use the join() method to convert it to a string before comparing.

e.g.

if(myName==hits.join(''))

Working code:

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.join(''))
    {
        hits=[];
        count=count+1;
    }
    hits=[];
}
if(count===0)
    console.log("Name not found!");
else
    console.log(count);

For more efficient ways of getting the number of occurrences have a look at the answers at How to count string occurrence in string?

H77
  • 5,859
  • 2
  • 26
  • 39
1

you can use regex to search your name, it's very simple:

var name = "abhishek apolo bpple abhishek";
var regexName = /abhishek/g;
var matchname = name.match(regexName);
if(matchname === null) {
    console.log("Name not found!");
} else {
    console.log(matchname.length);
}
YakovL
  • 7,557
  • 12
  • 62
  • 102
Selvakumar
  • 527
  • 2
  • 13
1

A neat approach using Array#reduce would be:

const text = 'History repeats itself. History repeats itself. Historians repeat each other.'

function count(needle, haystack) {
  return haystack
    .split(' ')
    .reduce((c, e) => e === needle ? ++c : c, 0)
}

console.log(count('repeats', text))
console.log(count('repeat', text))
.as-console-wrapper { max-height: 100% !important; top: 0; }

EDIT: As supposed jsperf shows that this solution is slower than the Regexp one .

Diego
  • 816
  • 7
  • 17
0

Simply do with split() .myName is string hits is a array both not equal for any condition

var text = "abhishek apolo bpple abhishek";
var myName = "abhishek";

var count = text.split(myName).length - 1
if (count === 0) {
  console.log("Name not found!");
} else {
  console.log(count);
}
prasanth
  • 22,145
  • 4
  • 29
  • 53
0

What about regexp ?

var text="abhishek apolo bpple abhishek",
  myName="abhishek",
  hits=[],
  regexp = new RegExp(myName,'g'),
  found ;

// Thanks to https://stackoverflow.com/a/6323598/2846837
do {
  found = regexp.exec(text) ;
  if ( found )
    hits.push(found[2]) ;
} while ( found ) ;

console.log(myName+' found : '+hits.length) ;
Pierre Granger
  • 1,993
  • 2
  • 15
  • 21
0
var text="abhishek apolo bpple abhishek",myName="abhishek",hits=[];

var array_text =  text.split(" ");
var count = 0
for(var i in array_text){
     if(myName == (array_text[i])){
    count++;
    hits.push(array_text[i]);
   }
}
if(count===0)
    console.log("Name not found!");
else
    console.log(count);
KARAN LAGALWAR
  • 227
  • 1
  • 8