-4

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"]
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
  • 5
    Smells like homework. Care to share the loop you've built? – CheeseFry Apr 18 '16 at 00:52
  • Is there a way to flag such question as "asking to solve homework while showing no effort at all" ? – Julien Apr 18 '16 at 01:05
  • Possible duplicate of [Syntax for a for loop in ruby](http://stackoverflow.com/questions/2032875/syntax-for-a-for-loop-in-ruby) – Stepango Apr 18 '16 at 02:12
  • Its actually for an admission question where we were to get as far as we could on our own, and then use any resources we could find for help. Im sorry if this is not the place for those types of questions. Im very new at this. – Scott Schrier Apr 18 '16 at 14:30

2 Answers2

2

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 }
philip yoo
  • 2,462
  • 5
  • 22
  • 37
1

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

Dharmesh Rupani
  • 1,029
  • 2
  • 12
  • 22