Essentially, I desire the function to read all letters from a string, and to spit out an object that contains {'letter', 'count'} properties for each letter.
function freqLetters(string) {
var freq = [];
for (var i = 0; i < string.length; i++) {
var character = string.charAt(i);
if (typeof freq[character] != undefined) {
freq[character].count++;
} else {
freq.push({'letter': character, 'count': 1});
}
}
return freq;
}
However, freq usually appears empty and when it works, it seems to always fail the '!=== undefined' check and push duplicate letters of 1 count, rather than finding them and increment them.
Is the typeof freq[character] !=== undefined] check correct?
How on earth do I increment dynamic elements?
(i.e. find letter:a if it exists, and increment its count by 1 if it exists)
if(typeof freq[character] != "undefined")....freq[character].count++;and all sorts of while testing, and it seems to just append to the array as if it's a new letter each time(so likely the!= undefinedstill fails) (oops I fixed the ] typo)if, the!= "undefined"part is correct. The problem is with the left part.