I am working on code of frequncy counter where I count the frequncy of each word from a given string.
I am creating an object and making every word as key and it's frequency as value to make key-value pair.
function wordCount(str) {
tempStr = str.toUpperCase()
arr1 = tempStr.split(" ")
let frequencyConter1 = {}
for (let val of arr1) {
frequencyConter1[val] = (frequencyConter1[val] || 0) + 1
}
for (key in frequencyConter1) {
console.log(key, frequencyConter1[key])
}
}
wordCount("My name is Xyz 1991 He is Abc Is he allright")
1991 1
MY 1
NAME 1
IS 3
XYZ 1
HE 2
ABC 1
ALLRIGHT 1
why 1991 goes to first position in output?
It should be after XYZ, isn't it?