Created
August 18, 2023 23:58
-
-
Save germanescobar/1a6c71d77d495b17e1483cc8ea3d2007 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var mostCommonWord = function(paragraph, banned) { | |
const words = paragraph.toLowerCase().split(/\W+/) | |
const freqs = frequencies(words, banned) | |
return findMaxWord(freqs) | |
}; | |
function frequencies(words, banned) { | |
const freqs = {} | |
for (let w of words) { | |
if (w && !banned.includes(w)) { | |
freqs[w] = freqs[w] ? freqs[w] + 1 : 1 | |
} | |
} | |
return freqs | |
} | |
function findMaxWord(freqs) { | |
let max = 0 | |
let maxWord = "" | |
for (let w in freqs) { | |
if (freqs[w] > max) { | |
max = freqs[w] | |
maxWord = w | |
} | |
} | |
return maxWord | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment