7. Let's learn return issue


#1



Whenever I run the code, and I type in the 2 numbers i wish to add, it just combines them rather than adding them. Fore example, if I type in the number: 3, and then the number: 5, it will spit out 35 for me, rather than 8.


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 }


#2

@designmaster90245
And if try it with

text="35"
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 }

Reference
http://docs.ruby-lang.org/en/2.0.0/String.html#method-i-split
http://stackoverflow.com/questions/1333347/how-to-use-stringsplit-in-ruby
http://stackoverflow.com/questions/15914614/how-do-i-split-string-into-array-of-multiple-words
http://stackoverflow.com/questions/7622369/ruby-extracting-words-from-string


#3

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