Javascript, name lookup
I am making a program in javascript that looks up my name in the text and logs its frequency to the console. I check each letter of the text first, and when it matches my first letter name, I use another one to loop, which pushes the letters into an array called hits. The letters from the string "text" are pushed to the length of my name using push (). After that, I check if the arrays "hits" and the string "myName" are equal, and if they are equal, I increment the count by one. But my code doesnβt work and I donβt know why, I thought about it very much, but it was all 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);
source to share
your code doesn't work because you are comparing an array to a string, which would give you false, you can get a string from a string of the array with join()
, or is it better to use a regex like:
var text="abhishek apolo bpple abhishek",myName="abhishek",hits=[];
var count=0;
console.log((text.match(new RegExp(myName, 'g'))).length);
source to share
Convenient approach using Array#reduce
:
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 this solution is slower than Regexp.
source to share
You are comparing an array to a string. Use the method join()
to convert it to a string before comparing.
eg.
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 take a look at the answers How to count occurrences of a string in a string?
source to share
Just use split()
. myName
is string hits
is an array, 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);
}
source to share
How about a regular expression?
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) ;
source to share
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);
source to share