101 - Medium_2 Exercises Flashcards
Steps to figure out the total age of the males in the Array:
munsters = {
“Herman” => { “age” => 32, “gender” => “male” },
“Lily” => { “age” => 30, “gender” => “female” },
“Grandpa” => { “age” => 402, “gender” => “male” },
“Eddie” => { “age” => 10, “gender” => “male” }
}
1) create an empty new variable; total_male_age = 0
2) loop the munsters array, / do…end
3) assign key, value pairs to a variable inside pipes |k, v|
k = key / v = details
4) point to values , [details] “age” and “male”
5) add details[“age”] if details[“gender”] == “male”
6) assign variable: total_male_age += details[“age”] if details[“gender”] == “male”
Given this previously seen family hash, print out the name, age and gender of each family member:
munsters = {
“Herman” => { “age” => 32, “gender” => “male” },
“Lily” => { “age” => 30, “gender” => “female” },
“Grandpa” => { “age” => 402, “gender” => “male” },
“Eddie” => { “age” => 10, “gender” => “male” }
}
…like this:
(Name) is a (age) year old (male or female).
(Name) is a (age) year old (male or female).
munsters.each_pair do |name, details|
puts “#{name} is a #{details[‘age’]} year old #{details[‘gender’]}”
end
Split the “sentence” up and reverse the order?
sentence = “Humpty Dumpty sat on a wall.”
words sentence.split
new_words = words.reverse!
final_words = new_words.join(‘ ‘)
Figure out the total age of the males in the Array:
munsters = {
“Herman” => { “age” => 32, “gender” => “male” },
“Lily” => { “age” => 30, “gender” => “female” },
“Grandpa” => { “age” => 402, “gender” => “male” },
“Eddie” => { “age” => 10, “gender” => “male” }
}
total_male_age = 0
munsters.each do |name, details|
total_male_age += details[“age”] if details[“gender”] == “male”
end