Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save sheland/2a5cb9de7580adef0664928548545c46 to your computer and use it in GitHub Desktop.

Select an option

Save sheland/2a5cb9de7580adef0664928548545c46 to your computer and use it in GitHub Desktop.
#Day 6 refactor your original account generator code to utilize arrays with hashes.
#Complete the following refactor steps:
#Utilize a single array variable to store all student information,
#instead of three individual arrays. This array will contain many hashes.
#Utilize a single loop to drive the hash population
#Update the printing functionality to utilize this new hash variable to print out student roster
new_array = []
#Inputs student's name 5x names
5.times do |num|
print "Please enter a first and last name:"
name = gets.chomp.upcase
#Generates 5 random numbers
5.times do
number = rand(111111..999999).to_s
#Generates 5 email address based on user's names
email = ""
first_name = name[0]
first_initial = first_name[0]
last_name = name.split.last
email = email + first_initial + last_name
last_three_digits = number.to_s[-3..-1]
email = email + last_three_digits + "adadevelopersacademy.org"
grouped = {student_names: name, numbers: number, email_address: number }
new_array << grouped
end
#output names, id, and emails
puts "Names:"
5.times { |num| puts new_array[num][:student_names] }
puts "Student IDs:"
5.times { |num| puts new_array[num][:numbers]}
puts "Email addresses:"
5.times { |num| puts new_array[num][:email_address]}
end
@shrutivanw
Copy link

The current code does not work as expected. I highly recommend getting into the practice of indenting your code according to Ruby style guidelines (this will help you spot whether your loops end where you expect them to and fix those bugs), and always testing your code with at least 3 different runs (this will help you always put your best foot forward before anyone else - instructors or future software developer coworkers review your code).

Here's what I see in the console on running the program as it stands now:

Please enter a first and last name:Ari Herman
Names:
ARI HERMAN
ARI HERMAN
ARI HERMAN
ARI HERMAN
ARI HERMAN
Student IDs:
507047
428748
731917
636930
172771
Email addresses:
507047
428748
731917
636930
172771
Please enter a first and last name:

Slack me once you have updated your code to show your best, revised attempt at this exercise. You got this! :-)

@CheezItMan
Copy link

Shruti has highlighted the bugs in your code. Feel free to let me know if you have questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment