Created
September 23, 2018 14:43
-
-
Save Luke-Rogerson/f77bbb351025a6f500559b6cd388ab9b to your computer and use it in GitHub Desktop.
mostFrequentWordChallenge created by Luke_Rogerson - https://repl.it/@Luke_Rogerson/mostFrequentWordChallenge
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 _ = require('lodash'); | |
function highestWordCount (str) { | |
let resultArray = str.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()0-9]/g, "").split(' '); | |
let words = {}; | |
for (word of resultArray) { | |
let counter = 0; | |
for (word2 of resultArray) { | |
if (word === word2) { | |
counter++; | |
words[word] = counter | |
} | |
} | |
} | |
let sortedByValue = _.transform (words, function(result, value, key) { | |
(result[value] || (result[value] = [])).push(key); | |
}, {}); | |
return (sortedByValue[Object.keys(sortedByValue)[Object.keys(sortedByValue).length - 1]]); | |
} | |
highestWordCount('I see, mmm... I see, 2 and 2.'); |
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
{ | |
"requires": true, | |
"lockfileVersion": 1, | |
"dependencies": { | |
"lodash": { | |
"version": "4.17.11", | |
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", | |
"integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A simple function to return the most frequent word (or words) in a string.