Iterating over the Array. My code works but cant pass Exercise 5


puts "Write a string:"
text = gets.chomp

words = text.split

frequencies =
frequencies.each do frequencies|word|
word = {"frequencies" => +1}
word = {"frequencies" => 2}
word = {"frequencies" => 3}
puts word["frequencies"]

this code works and prints the statement to the console however, it wont let me pass. Following the example didnt work either. 2 days on this exercise, help please ?



You do need parethesis after

like text.split()
Also, can you tell me your error message?


thanks! and sure one moment I need to copy the error message.


Oops, try again. It looks like you haven't added any key-value pairs to the frequencies hash.


Okay. Well I used a different constructor then you, but the big difference is that I just used frequencies[word] += 1
After that, you need to sort, and also use the reverse method.

frequencies.each do |word, frequency|
  puts word + " " + frequency.to_s

You want it to look somewhat like that.


Actually, that's more towards the end, after you use text.split(), you have to iterate over words, and then iterate over frequencies.

words.each { |word| frequencies[word] += 1 }
frequencies = frequencies.sort_by {|a, b| b}
frequencies.each do |word, frequency|
    puts word + " " + frequency.to_s


thank you for your help, I will try that :slight_smile:


Alright, let me know how it works


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.