5. Iterating over the array


#1

I am not sure what is wrong with my code. My code works as I intended and I think it did what the instruction asked me to do. What am I doing wrong?

Instruction :

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.

my code :

puts "Input your statement human"
text = gets.chomp
words = text.split(" ")
frequencies = Hash.new(0)
words.each do
    |word| frequencies["word"]+= 1
end
puts frequencies["word"]

#2

New to this and just finished this lesson myself, but I think the issue is just the " " around word twice at the bottom, making it think it's a string? Looks great (to me) otherwise!


#3

Hello, you need to remove the quotaion marks from word.

words.each do |word| frequencies[word] +=1
end
puts frequencies[word]


#4

Okay well first of all,

This is all done correclty, however, you made a mistake that occurs for many people. You have the word "words" plural in some places, and singular in others. I hope that makes sense. What you should do is take out all the letter "s" in the word, "word."


#5

This is not the error. The variable word is just a placeholder for each single word found within the sentence held in variable words. The problem is that the OP has written |word| frequencies["word"]+= 1 - with "word" instead of word, which makes the interpreter take the literal string value "word" instead of the string value contained in the variable word.


#6

Hi everyone!
I am having an issue with this code.
Can someone tell please what would be the exact code to pass the lesson?
I tried everything I could think of, couldn't pass.

Thank you!


#7

Can you please check what's wrong here:

puts "Input your statement human"
text = gets.chomp
words = text.split(" ")
frequencies = Hash.new(0)
words.each do
    |word| frequencies[word]+= 1
end
puts frequencies[word]

I am not sure what else to do to pass this lesson, have been working on it for a while.Maybe something wrong with the system.
Thanks!


#8

You don't need that statement.


#9

Thanks! It still doesn't let me pass, even if I remove the statement.


#10

This weird: i replaces first 2 lines with
text= "L L L K K A A A B C"
and it worked!


#12