This might sound confusing, but I’ve been stuck on Unit 4 for days, and I’m hoping someone can help me. I was able to advance from exercise 6 to 7, even though it shows me an error, but it let me continue.
First, here is my code:
puts "Your text here: "
text = gets.chomp
words = text.split
frequencies = Hash.new(0)
words.each { |word| frequencies[word] += 1 }
frequencies = frequencies.sort_by {word, count}
frequencies.reverse!
The error I see on the console is:
(ruby):7: syntax error, unexpected ‘}’, expecting ‘=’
(ruby):9: syntax error, unexpected $end, expecting ‘}’
But then at the bottom says “Way to Go!, Start New Lesson”
As a test, I used Codecademy code from Lesson 1 of this unit, called: “What you’ll be building”, and run it on the last exercise (Ex7):
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.reverse!
frequencies.each { |word, frequency| puts word + " " + frequency.to_s }
But even though it outputs it correctly, an alert message pops up and says “The program took too long to finish. Check your code for infinite loops and try again.”
1 - Why is this happening?, Why does this code work on exercise 1, but if you use it at the end, it creates an infinite loop?
2 - There is a line that I can’t seem to get past from the beginning of this unit:
frequencies = frequencies.sort_by {|a, b| b }
When I encounter it on exercise 6 says: “Use .sort_by to sort the frequencies hash by word count, like step 2 above. Store the result back in frequencies.” So why would it work using letters a, and b {|a, b| b }, as in the code provided in lesson 1?, I used “{word, count}”, and it gives me errors, why is this happening?
Hope someone can help!