I am trying to create an algorithm for word count in JavaScript, I am creating a for-in loop to fetch each word from array and then pushing them to the object but getting error while I am setting each object key to each array value.
const wordCount = input => {
let newArr = input.split(" ");
let obj = {};
for (let i in newArr) {
console.log(newArr[i]); // This returns value
obj.newArr[i] = 1; // This gives error:- Uncaught TypeError: Cannot set property '0' of undefined
}
console.log(obj);
};
wordCount("heyy how you doin heyy");
const wordCount = input => {
let newArr = input.split(" ");
let obj = {};
for (let i in newArr) {
// console.log(newArr[i]); // This returns value
obj[newArr[i]] = 1; // This gives error:- Uncaught TypeError: Cannot set property '0' of undefined
}
console.log(obj);
};
wordCount("heyy how you doin heyy");