Last active
February 8, 2017 05:31
-
-
Save pablodenadai/80dbc916d11bd39a134239c97c87ad21 to your computer and use it in GitHub Desktop.
Calculate the mode of a given string - JavaScript ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mode = (str) => { | |
return str | |
.split(' ') | |
.reduce((data, key) => { | |
let counter = data.map[key] + 1 || 1 | |
data.map[key] = counter | |
if (counter > data.counter) { | |
data.counter = counter | |
data.mode = key | |
} | |
return data | |
}, { | |
counter: 0, | |
mode: null, | |
map: {} | |
}) | |
.mode | |
} | |
console.log(mode('the t-rex is the greatest of them all')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment