Given an Array with some words in it, loop through the Array to display the number of characters in each word.
friends = ["Dan", "Mindy", "Suhasini", "Ryan"]
Given an Array with some words in it, loop through the Array to display the number of characters in each word.
friends = ["Dan", "Mindy", "Suhasini", "Ryan"]
If you're looking to return an array with length displayed, the following would work:
friends = ["Dan", "Mindy", "Suhasini", "Ryan"]
p friends.map { |friend| friend.length }
You can display the number of characters using an array as following.
friends = ["Dan", "Mindy", "Suhasini", "Ryan"]
def count_char(friends)
count = 0
friends.each do |word|
count = word.length
puts "'#{word}' has #{count} character(s)"
end
end
count_char(friends)
=>
'Dan' has 3 character(s)
'Mindy' has 5 character(s)
'Suhasini' has 8 character(s)
'Ryan' has 4 character(s)
For more array reference : http://ruby-doc.org/core-2.2.0/Array.html