Sorting the Hash Help


#1

Hi.

I'm having trouble with this section.

Below is my code. And I keep getting the error:

"Oops, try again. It looks like you haven't set the frequencies hash correctly. Check the first exercise if you need help!"

But I got the first exercise OK... I mean obviously since I'm on the 6ht one...

Can someone please explain what I'm doing wrong and WHY it's wrong?


puts "Type something"
text = gets.chomp

words = text.split(" ")
frequencies = Hash.new(0)
words.each do |words|
    puts frequencies [words]
    frequencies[words] += 1
    frequencies[words] = frequencies.sort_by |words, count|
    count   
end
frequencies.reverse!


#2

Hello @devwhiz15151,

To sort the frequencies hash, you have to have another .each loop, and store the result of that in the frequencies hash


#3

Here is my code:
puts "Text:"
text = gets.chomp
words = text.split
frequencies = Hash.new(0)
words.each do |word|
frequencies[word] +=1
puts frequencies[word]
end
frequencies = frequencies.sort_by do |word, count|
frequencies
end
frequencies.reverse!