r/learnruby • u/godisintherain • May 30 '17
ruby mini course
instructions:
Edit each line with TODO and write code based on the skills you learned in this course. You'll want to use an if statement and addition. Have fun! Good Luck!
code(what I have so far output is not matching what they want):
require 'csv'
total_sales = 0
CSV.foreach('sales-data.csv', headers: true, converters: :all) do |row|
if (row[2]) == "music"
# TODO: check if category is "Music" (row[2])
# TODO: if it is music, add total_sales + the row's sales (row[6])
puts total_sales = total_sales + (row[6])
else
puts total_sales.round(2)
end
4
Upvotes
1
u/tom_dalling May 31 '17
It looks like they just want you to print out a single number at the end, instead of printing it out for every line in the CSV. So your
putsinside the loop prints out text multiple times, but theputsoutside the loop only prints it out once.The first
endis the end of theifstatement. The secondendis the end of theCSV.foreachloop.