Iterating Over the Hash... can not pass this excersize at all please some one HELP


#1

I BEEN IN THIS PLACE FOR MORE THAN 4 HOURS AND I GET THE SAME ERROR "Oops, try again. It looks like you aren't printing each key-value pair to the console correctly. Make sure to put a space between each key and value! "
THIS IS MY CODE I DONT KNOW WHAT IS WRONG AT ALL.. IVE TRY EVERYTHING ON THE FORUM BUT NOTHING IS HELPING SINCE I GET THE SAME ERROR ALL THE TIME. PLEASE HELP

puts "whatever"
text = gets.chomp
words = text.split (" ")
puts "whenever"
frequencies = Hash.new(0)
words.each { |word| frequencies[word] += 1}
frequencies = frequencies.sort_by {|a, b| b}
frequencies.reverse!
frequencies.each do |word, count|
puts word + " " + count.to_s
end

WHATS IS WRONG WITH THIS LESSON????/ CAN NOT PASS IT AT ALL PLEASE SOMEONE HELP


#2

Browser issue. I cut and paste your code into Firefox and it passes.


#3

Same here, tried your code and it passes. I've seen some people having troubles because of the cache of their browser, try emptying it.


#4

thanks for your help but how do i empty the cahe of my browser?
cause i did log on to try and finished my code and still the same error i am using firefox
can u help me please
thanks


#5

https://support.mozilla.org/en-US/kb/how-clear-firefox-cache?redirectlocale=en-US&redirectslug=How+to+clear+the+cache


#6

i did clear now the cache of the browser on firefox and still the same message error
"Oops, try again. It looks like you aren't printing each key-value pair to the console correctly. Make sure to put a space between each key and value! "
this is my code:

puts "whatever"
text = gets.chomp

words = text.split (" ")
puts "whenever"

frequencies = Hash.new(0)

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

frequencies = frequencies.sort_by {|a, b| b}
frequencies.reverse!

frequencies.each do |word, count|
puts word + " " + count.to_s
end


#7

I have had the same error since Create a Histogram: exercise 4.
They just made something confusing 10x more confusing. I think that may just mean no more coding for today :weary:


#8

I'm guessing because you use frequencies and count... sweet ruby doesn't know what you are telling her. (same with word, word != word)

try:

frequencies.each do {| words, frequencies| puts words + " " + frequencies.to_s }


#9

First off, I know how frustrating this must be. I almost ripped my hair out.

Check how you set values and how you call your values

word is not the same as words
frequencies is not the same as count

ruby is saying "wtf, where did these things come from?" You didn't tell her what count or word was.


#10

@casielane

I'm not sure what you're trying to tell him, but how he names his variables inside the each method is a bit irrelevant.

It can work fine with count and word (or with yolo and yili). You can test it out yourself if you want.

I completely forgot to follow this thread it seems. I think his last code does the job, but the exercise check may have a problem with:

puts "whenever"

#11

Try this:

puts "Text please: "
text = gets.chomp

words = text.split(" ")
frequencies = Hash.new(0)
words.each { |word| frequencies[word] += 1 }
frequencies = frequencies.sort_by { |k, v| v }
frequencies.reverse!
frequencies.each do |word, frequency|
puts word + " " + frequency.to_s
end


#12

Like you I was going crazy with this, trying everything, emptied my cache, put in "fruit", took out "fruit", then finally in desperation I removed everything below 'frequencies.reverse!', hit "Submit" and it passed me...try that...too, in the next exercise I hit "Submit" again and it gave me the proper numbers, passed me again...there's definitely weirdness afoot...


#13

Can someone....anyone please help me....I am sooooo lost! I have tried various combinations etc and still I cannot pass!

Here is my code:

puts "whatever"
text = gets.chomp

words = text.split (" ")
puts "whenever"

frequencies = Hash.new(whatever)

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

frequencies.each do {| words, frequencies| puts words + " " + frequencies.to_s }
end

This is what I get when I try to save and submit:

"Your code doesn't look quite right. Check the Hint if you need help!"

I have been stuck on this for 5 days now....any suggestions/advice would be well appreciated! Thankyou in advance!


#14

Try this:

puts "whatever"
text = gets.chomp

words = text.split (" ")

frequencies = Hash.new(0)

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

frequencies.each do {| words, frequencies| puts words + " " + frequencies.to_s }
end

(Instructions
Use .each to iterate over the words array.
For each word we find, assume that the word itself is a key in frequencies and increment its value by 1.
This is why our default is 0. The first time we find the word, it will have a default value of 0 that we can increment up to 1.)
In your code, ( puts "whenever") , seems to be a prompt for the user to provide the word count which I think is the wrong approach because it's you who is supposed to provide that answer using your code.


#16

Change frequencies = Hash.new(0) to frequencies = Hash.new[0]


#17

I did the following and it passed me.

puts "whatever"
text = gets.chomp
words = text.split (" ")
puts "whenever"
frequencies = Hash.new(0)
words.each { |word| frequencies[word] += 1}
frequencies.each do |word, count|
puts word + " " + count.to_s
end


#18

here im slove the problem
puts "whatever"
text = gets.chomp

words = text.split (" ")
puts "whenever"

frequencies = Hash.new(0)

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

frequencies = frequencies.sort_by {|a, b| b}
frequencies.reverse!

frequencies.each do |word, count|
puts word + " " + count.to_s
end


#19

;;iuouyuygoliyci;crzysdfghjkl;sedrftgyhujikol