Skip to content
Advertisement

word frequency in javascript

enter image description here

How can I implement javascript function to calculate frequency of each word in a given sentence.

this is my code:

JavaScript

am unable to trace out the problem ..any help is greatly appriciated. output in this format: count of is – 1 count of the – 2..

input: this is anil is kum the anil

Advertisement

Answer

I feel you have over-complicated things by having multiple arrays, strings, and engaging in frequent (and hard to follow) context-switching between loops, and nested loops.

Below is the approach I would encourage you to consider taking. I’ve inlined comments to explain each step along the way. If any of this is unclear, please let me know in the comments and I’ll revisit to improve clarity.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement