Reputation: 367
I'm working on a script to create metrics for online author identification. One of the things I came across in the literature is to count the frequency of each letter (how many a's, how many b's, etc) independent of upper or lower case. Since I don't want to create a separate statement for each letter, I'm trying to loop the thing, but I can't figure it out. The best I have been able to come up with is converting the ASCII letter code in to hex, and then...hopefully a miracle happens.
So far, I've got
element = id.toLowerCase();
var hex = 0;
for (k=97; k<122; k++){
hex = k.toString(16); //gets me to hex
letter = element.replace(/[^\hex]/g, "")//remove everything but the current letter I'm looking for
return letter.length // the length of the resulting string is how many times the ltter came up
}
but of course, when I do that, it interprets hex as the letters h e x, not the hex code for the letter I want.
Upvotes: 2
Views: 116
Reputation: 53301
Not sure why you'd want to convert to hex, but you could loop through the string's characters and keep track of how many times each one has appeared with an object used as a hash:
var element = id.toLowerCase();
var keys = {};
for(var i = 0, len = element.length; i<len; i++) {
if(keys[element.charAt(i)]) keys[element.charAt(i)]++;
else keys[element.charAt(i)] = 1;
}
You could use an array to do the same thing but a hash is faster.
Upvotes: 5