repl.it
@mcuringa/

MapLetterFrequency

ES6

An example of using Javscript objects as a map in order to count the letter frequency in a text.

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let letterCounts = {};
let word = "Mississippi";
word = word.toLowerCase();
for(let i = 0; i < word.length; i++) {
  let letter = word[i];
  if(letterCounts[letter]) {
    letterCounts[letter]++;
  }
  else {
    letterCounts[letter] = 1;
  }
}
console.log(letterCounts);

let keys = Object.keys(letterCounts);
keys.sort();
for(let i = 0; i < keys.length; i++) {
  let letter = keys[i];
  console.log(`${letter}: ${letterCounts[letter]}`);
}
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie
?