+ 1

Help counting how many "e" appear in an array of strings

I am trying to figure out how to refine this code to make it count how many of the letter "e" appears throughout an array. The way the code is right now, it will count the number of "e" and add it the the counter IF there is only one string in (word). I can not figure out how to keep my loop iterating over word.length and also iterating through the length of the individual string while checking each index for a == "e". Any help would be greatly appreciated. Basically i want to count all of the "e" that appear in the method calls and return the total count def count_e(word) count = 0 i = 0 while i < word.length char = word[i] if char == "e" count += 1 end i += 1 end return count end puts count_e("movie", "electricity") # => 3 puts count_e("excellent", "elf", "elephant") # => 6

16th Jun 2019, 10:18 AM
Stephen Nowak
Stephen Nowak - avatar
3 Réponses
+ 3
If I am correct your method takes one string: I would try this: input 3 -> call method 3 times -> sum each returned value Or: Add all input to one String. E.g. excellent + elf + elephant = excellentelfelephant
16th Jun 2019, 11:04 AM
Denise Roßberg
Denise Roßberg - avatar
+ 3
Stephen Nowak Your welcome :)
16th Jun 2019, 4:58 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Denise Roßberg, idk why i didnt think about combining them all into one mega string before performing the count. Maybe i was just staring at the problem for too long lol. Thanks for your help.
16th Jun 2019, 4:36 PM
Stephen Nowak
Stephen Nowak - avatar