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


#1

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

words = text.split

frequencies = Hash.new(0)
frequencies.each do frequencies|word|
word = {"frequencies" => +1}
word = {"frequencies" => 2}
word = {"frequencies" => 3}
puts word["frequencies"]
end

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 ?

`


#2

You do need parethesis after

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


#3

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


#4

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


#5

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
end

You want it to look somewhat like that.


#6

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.reverse!
frequencies.each do |word, frequency|
    puts word + " " + frequency.to_s
end

#7

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


#8

Alright, let me know how it works


#9

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