// wordCount.js -- tiny word-count app in JavaScript. // Splits a paragraph into words, counts totals, uniques, and top frequencies, // then emits a JSON summary via jsonStringify. // Run: bin/calog examples/scripts/apps/wordCount.js const paragraph = "the quick brown fox jumps over the lazy dog " + "the dog was not amused and the fox did not care " + "the quick fox and the lazy dog became the best of friends"; // Normalize to lowercase and split on any run of non-letters. const words = paragraph .toLowerCase() .split(/[^a-z]+/) .filter((w) => w.length > 0); // Tally frequency of each word. const freq = {}; for (const w of words) { freq[w] = (freq[w] || 0) + 1; } const uniqueWords = Object.keys(freq); // Rank words by count (descending), then alphabetically for stable ties. const ranked = uniqueWords.slice().sort((a, b) => { if (freq[b] !== freq[a]) { return freq[b] - freq[a]; } return a < b ? -1 : a > b ? 1 : 0; }); const topFive = ranked.slice(0, 5).map((w) => ({ word: w, count: freq[w] })); const summary = { totalWords: words.length, uniqueWords: uniqueWords.length, top: topFive, }; calogPrint(jsonStringify(summary)); calogExit(0);