Lesson 5 - Iterating Over the Array - Help


#1

I received an error message stating that I haven't added key-value pairs to the frequencies. I have the following code:

puts "How many licks does it take to get to the Tootsie Roll center of a tootsie pop?"
text = gets.chomp

words = text.split (" ")
frequencies = Hash.new(0)
words.each do |word|
frequencies[word] +=1
puts frequencies[word]
end

What am I doing wrong? What do I need to do?

Replace this line with your code.


#2

@dtcarter84,

text= "L L L K K A A A B C"
words = text.split (" ")
frequencies = Hash.new(0)
words.each do |word|
 frequencies[word] +=1
end
puts frequencies

#3

Thank you! That worked. Did I have the code set up incorrectly?


#4

I was reviewing the forum for this lesson and some used the code I used and it worked fine, but it didn't with others.