Lesson 5 help


Hi I’m really stuck on this lesson i have tried the code at the start and not even that works please help


heres my code `puts “Text please: “
text = gets.chomp

words = text.split(" ")
frequencies = Hash.new(0)
words.each { |word| frequencies[word] += 1 }
frequencies = frequencies.sort_by {|a, b| b }
frequencies.each { |word, frequency| puts word + “ “ + frequency.to_s }`


Mine could give you some ideas as to why yours is failing;

puts "Please type a string."
text = gets.chomp

words = text.split

frequencies = Hash.new(0)

words.each {|var| frequencies [var] += 1}

In lesson 5 they are asking you to iterate over your array, so you would use .each to loop back, then define what you're doing with that iteration. In my example I gave that iteration a placeholder variable of 'var' and then said that I wanted to loop it with the frequencies hash value PLUS 1.

When you do this, it tells your code that you're asking it to add +1 to the second key for each entry in the frequencies hash. I know it's confusing, I'm still working it out myself, but I hope that helped.


thanks it worked :grin: