Explorer
Node.js

Write a function `wordFrequency(str)` that returns a Map with word frequencies. Convert to a plain object for output.

Problem Statement

Write a function `wordFrequency(str)` that returns a Map with word frequencies. Convert to a plain object for output.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: Split the string by spaces to get words. šŸ’” Hint 2: Use a Map to store word counts. Map.get() and Map.set() for access. šŸ’” Hint 3: Convert Map to object: Object.fromEntries(map). āœ… Solution: ```javascript function wordFrequency(str) { const map = new Map(); str.split(" ").forEach(word => { map.set(word, (map.get(word) || 0) + 1); }); return Object.fromEntries(map); } ```

Map Data Structure

Easy
Write a function `wordFrequency(str)` that returns a Map with word frequencies. Convert to a plain object for output.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
{"the":2,"cat":1,"sat":1,"on":1,"mat":1}