Ruby Loop Sum Need Help ASAP


#1

So basically I have to make a “Menu” and it askes you to enter all the item you want until you enter “0”. When you enter ‘0’ it should give you a total amount you owe…

I cant seem to get the sum of the orders…

this is what I have so far


menu_items = [“Hamburger”, “Cheeseburger”, “Larger Fry”, “Small Fry”,
“Fountain Drink”, “Chips”,“Hot Dog”, “Pop Corn”, “Candy Bar”,“EXIT MENU AND PRINT RECEIPT” ]
menu_items [0] == 2.25
menu_items [1] == 2.75
menu_items [2] == 1.75
menu_items [3] == 1.25
menu_items [4] == 1.00
menu_items [5] == 0.50
menu_items [6] == 2.00
menu_items [7] == 1.25
menu_items [8] == 0.75

puts menu_items

selection = []
until selection == 0
puts "Enter your selection: "
selection = gets.chomp.to_i
if selection == 0
sum = selection.map {|sum,e| + e.to_i }
end
end


#2
menu_items = ["Hamburger", "Cheeseburger", "Larger Fry", "Small Fry", 
"Fountain Drink", "Chips","Hot Dog", "Pop Corn", "Candy Bar", "EXIT MENU AND PRINT RECEIPT" ]
menu_items [0] == 2.25
menu_items [1] == 2.75
menu_items [2] == 1.75
menu_items [3] == 1.25
menu_items [4] == 1.00
menu_items [5] == 0.50
menu_items [6] == 2.00
menu_items [7] == 1.25
menu_items [8] == 0.75

puts menu_items

selection = []
until selection == 0
  puts "Enter your selection: "
  selection = gets.chomp.to_i
  if selection == 0
    sum = selection.map {|sum,e| + e.to_i }
  end
end

Hi @sdem9,

there are a few problems with your approach:

  1. if 0 is the exit mode, then why is it the last item in your Array? 0 would probably mean “Hamburger”.
  2. you’re using == to compare a string with a number: I guess you want to assign that number to this item.
  3. you declare selection to be an Array, but then you assign a number to it, so you lose it.
  4. you cross-posted :smirk:

So I suggest you do the following:

  1. create a Hash where the keys are the items, and the values the price
  2. use << instead of = when assigning your input to selection, so it’s actually an Array that you can map
  3. if 0 has to be your code to exit selection, then use an Array for menu_items but EXIT MENU... should be the first item (since it’s index 0).

Where does this bring you to?


#3

Thank you very much! I have not yet worked on the problem with your advice. I will pick back up tomorrow. But I will let you know (:

much appreciated again!


#4
hash = {}

menu_items = ["EXIT MENU AND PRINT RECEIPT", "Hamburger", "Cheeseburger", "Larger Fry", "Small Fry", "Fountain Drink", "Chips","Hot Dog", "Pop Corn", "Candy Bar"]

hash[menu_items[1]] = 2.25
hash[menu_items[2]] = 2.75
hash[menu_items[3]] = 1.75
hash[menu_items[4]] = 1.25
hash[menu_items[5]] = 1.00
hash[menu_items[6]] = 0.50
hash[menu_items[7]] = 2.00
hash[menu_items[8]] = 1.25
hash[menu_items[9]] = 0.75

puts menu_items

total = []
selection = nil
until selection == 0
  puts "Enter your selection: "
  selection = gets.chomp.to_i
  if selection.zero?
   total = total.reduce(0, :+)
   else
   price = hash[menu_items[selection]].nil? ? 0.0 : hash[menu_items[selection]]
   total << price
  end
end

puts total

This works
Suggestions:
1- Use simple quote (’) instead of comillable (") for strings
2- In this example I only add the price of the selected elements when you enter when converting the selection with to_i is 0


#5

I didn’t test it, but you get the idea.

Click to see solution
menu_items = [
  ["1 - Hamburger", 2.25],
  ["2 - Cheeseburger", 2.75]
]

puts menu_items.map {|item| item[0]}.join("\n")

selections = []
until selections.last == 0
  puts "Enter your selection: "
  selections << gets.chomp.to_i
end
selections.pop
total = selections.inject {|sum, item| sum + menu_items[item-1][1] }
puts "Total: %0.2f" % total

#6

Nice! @arcangel8801, @smprather1 could you please edit your posts to:

  1. add syntax highlighting (start the code block with ``` ruby on its own line, end it with ``` on its own line)
  2. hide the solution within a spoiler alert block
Click to see solution... Er, screenshot

2018-10-24-101302

Thank you so much!


#7

Sure how. My first post. Done! Thanks!


#8

My first post. Done! Thanks!


#9

Thank you so VERY much for your help.

I understand what you changed and why.

Once again thank you!


#10

I changed those silly mistakes and its working better! Im thankful you pointed those out