Skip to Content
Create a Histogram
Iterating Over the Array

Perfect! Next up: we want to iterate over words to add each word to our frequencies hash, one at a time.

colors = { "red" => 2, "blue" => 3 } colors["blue"] += 1 puts colors["blue"]
  1. In the above example, we first create a hash mapping strings to integers.
  2. Then, we increment the value stored by "blue" by 1.
  3. Finally, we print out 4, the value stored by "blue".



Use .each to iterate over the words array.

For each word we find, assume that the word itself is a key in frequencies and increment its value by 1.

This is why our default is 0. The first time we find the word, it will have a default value of0that we can increment by1`.

Take a look at the Hint if you need help.

Folder Icon

Take this course for free

Already have an account?