Immutability: why mutating shared state causes bugs

Mutating objects in place makes code hard to reason about — especially when multiple things share a reference to the same object. Immutable operations return new values instead of changing old ones.

You're given a sentence. Build a word-frequency map where each word maps to how many times it appears. Words are lowercase, punctuation stripped.

Then print each word and its count, sorted alphabetically, one per line in the format word: count.

Input:  "the cat sat on the mat the cat"
Output: cat: 2
        mat: 1
        on: 1
        sat: 1
        the: 3

The constraint: Do not mutate any variable once it's assigned. Build the result using only const/val/let-with-new-value patterns.


Constraints

  • All lowercase. Strip non-alphabetic characters (keep only a-z and spaces).
  • Output sorted alphabetically.
  • No mutation: every "update" must create a new object/value.
Ad SpaceGoogle AdSense active for guests & free-tier learners.