Home > Mobile >  I for some reason can't get this iteration thing right in my head
I for some reason can't get this iteration thing right in my head

Time:09-16

Once again I am a new student to this whole computer coding thing and I am doing a boot camp to try to get the basics and my foot in the door but for some reason I can't make this whole iteration thing stick in my brain we just started doing hashes in ruby and I have literally been staring at the checkpoint problem for a day and a half and I just can't make my brain know what the next logical step is to get the answer provided. It is in a pre work section before my actual live classes start here in a few weeks and it's only my second full week doing any coding at all so the most bare bones basic hints/answers would be greatly appreciated.

This is the problem: Write a loop to give each person an email address that consists of their first name last name @ gmail.com. For example, Robert Garcia will have an email of [email protected]. The program should end with: p people

people = [
  {
    "first_name" => "Robert",
    "last_name" => "Garcia", 
    "hobbies" => ["basketball", "chess", "phone tag"]
   },
   {
    "first_name" => "Molly",
    "last_name" => "Barker",
    "hobbies" => ["programming", "reading", "jogging"]
   },
   {
    "first_name" => "Kelly",
    "last_name" => "Miller",
    "hobbies" => ["cricket", "baking", "stamp collecting"]
   }
]

outer_index = 0
names = []
last_names = []
while outer_index < people.length
  names << people[outer_index]["first_name"].downcase
  last_names << people[outer_index]["last_name"].downcase
  outer_index  = 1
end 


  email = email = [names[0]   last_names[0]   "@gmail.com"]

this is all the farther I have gotten because everything I've tried to get it to go back trough and pick up the second and third names hasn't worked.

According to them this is what it is supposed to look like in the end: so that you can see if the correct modifications were made to each hash. The result should be:

people =[
  {
    "first_name" => "Robert",
    "last_name" => "Garcia", 
    "hobbies" => ["basketball", "chess", "phone tag"],
    "email" => "[email protected]"
   },
   {
    "first_name" => "Molly",
    "last_name" => "Barker",
    "hobbies" => ["programming", "reading", "jogging"],
    "email" => "[email protected]"
   },
   {
    "first_name" => "Kelly",
    "last_name" => "Miller",
    "hobbies" => ["cricket", "baking", "stamp collecting"],
    "email" => "[email protected]"
   }
]

(Note that your output won't be indented nicely).

I am completely at a loss and I cannot see where I am going wrong so any help would be insanely helpful so I can get through this checkpoint and finish up week two and move on to week three asap.

CodePudding user response:

You're really overcomplicating it, there is no need to use while to simply loop across an array. Instead use #each from the Enumerable module:

people.each do |hash|
  hash.merge!(
    "email" => "#{hash['first_name']}#{hash['last_name']}@gmail.com".downcase
  )
end

Or if you want a non-destructive version that doesn't alter the original data:

people.map do |hash|
  hash.merge(
    "email" => "#{hash['first_name']}#{hash['last_name']}@gmail.com".downcase
  )
end

CodePudding user response:

It's pretty straightforward to loop over each element of the people array. We also can use string interpolation to easily compose the email address.

people.each do |h| 
    h["email"] = "#{h["first_name"]}#{h["last_name"]}@gmail.com".downcase 
end

If we want to break this up a bit, we can.

people.each do |h| 
    fn = h["first_name"]
    ln = h["last_name"]
    h["email"] = "#{fn}#{ln}@gmail.com"
    h["email"].downcase!
end
  • Related