// Level 3, exercise 3 from https://github.com/Asabeneh/30-Days-Of-JavaScript/blob/master/02_Day_Data_types/02_day_data_types.md const sentence = '%I $am@% a %tea@cher%, &and& I lo%#ve %te@a@ching%;. The@re $is no@th@ing; &as& mo@re rewarding as educa@ting &and& @emp%o@weri@ng peo@ple. ;I found tea@ching m%o@re interesting tha@n any ot#her %jo@bs. %Do@es thi%s mo@tiv#ate yo@u to be a tea@cher!? %Th#is 30#Days&OfJavaScript &is al@so $the $resu@lt of &love& of tea&ching'; const cleaned = sentence.replace(/[^\w\d\s]/g, ''); const split = cleaned.split(/\s+/); // or: cleaned.match(/\w+/g) const wordFreqs = split.reduce((obj, item) => { obj[item] = obj.hasOwnProperty(item) ? obj[item] + 1 : 1; return obj; }, {}); function transposeObj(obj) { let transposed = {}; for (let k in obj) { if (transposed.hasOwnProperty(obj[k])) { transposed[obj[k]].push(k); } else { transposed[obj[k]] = [k]; } } return transposed; } function max(arr) { return arr.reduce((x,y) => x > y ? x : y); } const wordsByFreq = transposeObj(wordFreqs); const mostFreqWords = wordsByFreq[max(Object.keys(wordsByFreq))]; console.log(mostFreqWords);