Hello everyone,
I was thinking of the pseudo-code to count the frequency of characters in text. It was getting too long and I found this functions that do it, however, they are not perfect, but I’m not sure how to fix it. I’d appreciate if someone can walk me through the code. I’d like to write my own, but would be helpful to compare with other examples.
Number 1:
function letterFrequency(text){
var count = {};
string.split('').forEach(function(s) {
count[s] ? count[s]++ : count[s] = 1;
});
return count;
}
Number 2
function getFrequency(text) {
var freq = {};
for (var i=0; i<string.length;i++) {
var character = string.charAt(i);
if (freq[character]) {
freq[character]++;
} else {
freq[character] = 1;
}
}
return freq;
};
My pseudo code, with too many indentation levels:
function countLetters(text) count = {} if text.length > 2 previousLetter = text.ChartAt[0]; count = letter, 1 // how do I write this in js? for (ch.chartAt[1], ch < text.length - 1, ch++) nextCharacter = text(ch) if new character in count object count = ch +1 else add new ch count in count return count else count = ch, 1
Thanks a lot.