Skip to content

Instantly share code, notes, and snippets.

@raghubetina
Created January 30, 2013 20:22
Show Gist options
  • Save raghubetina/4676551 to your computer and use it in GitHub Desktop.
Save raghubetina/4676551 to your computer and use it in GitHub Desktop.
# Hard Challenge: Based on the data stored in the shopping_cart, sales_tax, and params variables below, write code that prints out the customer's total, including estimated sales tax, based on the shipping address they entered. Your code should work even if the values of the data change.
# Your output should look like this:
# Your total with tax is $4455.54.
shopping_cart = [
{'name' => "iPad 2", 'price' => 499, 'quantity' => 2},
{'name' => "iMac 27", 'price' => 1699, 'quantity' => 1},
{'name' => "MacBook Air 13", 'price' => 1299, 'quantity' => 1}
]
sales_tax = {"IL" => 0.115, "IN" => 0.09, "MI" => 0.06, "WI" => 0.056}
params = {
'name' => "Patrick McProgrammerson",
'address1' => "222 W. Merchandise Mart Plaza",
'address2' => "12th Floor",
'city' => "Chicago",
'state' => "IL",
'zip' => "60654"
}
# Hint: First try doing it yourself by hand, and notice the steps you are taking. You will have to translate these instructions into Ruby.
# Solution:
# Now change the value of the key 'state' in the params hash to "WI" and run your code again.
# Your output should look like this:
# Your total with tax is $4219.776.
# Continued below...
# Encapsulate your code in a method if you haven't already. The method should accept a cart array, a tax hash, and a customer hash as arguments.
def # call it print_total()
# Your code goes here.
end
# Now change the quantity of iMacs to 3 and run your method.
print_total(shopping_cart, sales_tax, params)
# Notice the differences. What had to match with what, now that we moved the logic into a method?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment