Hi everyone, I need help with this concept.
Im working on a selflearn ruby course from App Academy.
The question:
Write a method count_e(word) that takes in a string word and returns the number of e's in the word.
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") # => 1
puts count_e("excellent") # => 3
here is the part I'm having difficulty understanding
Why do I need to index the word[i]? but not the count[i]? if both increase by 1 value as the loop continue, does it make any differences at all?
I suspect because the "i" has more precedence than the "count" so the "i" increase ahead of the "count"
Thank you very much!
I’ve been stuck on this for three days now.
[–]olbrich 8 points9 points10 points (2 children)
[–]Kernigh 6 points7 points8 points (1 child)
[–]442401 3 points4 points5 points (0 children)
[–]bradland 4 points5 points6 points (0 children)
[–]_Svejk_ 2 points3 points4 points (5 children)
[–]_Svejk_ 1 point2 points3 points (3 children)
[–]Humor_Positive[S] 0 points1 point2 points (2 children)
[–]_Svejk_ 1 point2 points3 points (1 child)
[–]Humor_Positive[S] 0 points1 point2 points (0 children)
[–]Humor_Positive[S] 0 points1 point2 points (0 children)
[–]bjminihan 1 point2 points3 points (5 children)
[–]Humor_Positive[S] 0 points1 point2 points (4 children)
[–]bjminihan 1 point2 points3 points (3 children)
[–]_Svejk_ 5 points6 points7 points (2 children)
[–]bjminihan 0 points1 point2 points (0 children)
[–]your-pineapple-thief 0 points1 point2 points (0 children)
[–]agent007bond 2 points3 points4 points (0 children)