charFreq
const charFrequency = (string) => {
let count = {};
let results = [];
string.split('').forEach(function(c) {
count[c] ? count[c] += 1 : count[c] = 1;
});
var keys = Object.keys(count);
keys.forEach(function(key) {
results.push([key, count[key]])
});
return results
}
// const concatMap = (array, fn) => {
// return array.map(function(el) {
// return fn(el);
// }).concatAll();
// };
///\\// sort descending order by FREQUENCY\\
const sortDescByFreq = (a, b) => {
if (a > b) {
return 1;
}
if (a < b) {
return -1;
}
return 0;
};
const sortAscByChar = (a, b) => { ///// then sort by ASCENDING order character
return a - b;
}
var countMississippi = charFrequency("mississippi");
var sortedByFreq = countMississippi.sort(sortDescByFreq());
var sortedByChar = sortedByFreq.sort(sortAscByChar(a, b))
// [ [ "i", 4 ], [ "s", 4 ], [ "p", 2 ], [ "m", 1 ] ]
This Gist was automatically created by Carbide, a free online programming environment.