4 Replies - 1104 Views - Last Post: 20 February 2017 - 11:21 AM Rate Topic: -----

#1 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Trying to figure out how to get the value of the lowest price

Posted 17 February 2017 - 07:58 PM

Here is the code I have so far, i had to set the variables to 999 to see the rest of the code work but I'm a bit stuck on how to get the rest of it so i can change my variables from being 999..
 
elsif user_input == 6
   lowest = 999
   lowest_price = 999
    product.each do |k, v|
      if v[1] < lowest
        lowest = v[1]
        lowest_price = "[#{k}" + " " + "#{v[0]}" + " " + "#{v[1]}]"
      end
    end
    puts lowest_price



Is This A Good Question/Topic? 0
  • +

Replies To: Trying to figure out how to get the value of the lowest price

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: Trying to figure out how to get the value of the lowest price

Posted 18 February 2017 - 02:33 AM

Essentially, to avoid the magic number 999, you would treat the first element as the lowest one, then iterate the remaining elements, comparing them to this lowest.

You could either do this as separate steps, or have a single loop (across the collection) that has a condition to check if the current element is the first one.

# loop the collection
# is this the first item?
# - it is the lowest
# otherwise, compare to lowest
Was This Post Helpful? 0
  • +
  • -

#3 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Trying to figure out how to get the value of the lowest price

Posted 18 February 2017 - 03:29 PM

Ok, I'm sorry, I'm still real new at this, I'm not sure what you mean by all that, is there a way to dumb it down just a bit for me?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: Trying to figure out how to get the value of the lowest price

Posted 19 February 2017 - 01:20 AM

If I've understood, and you want to avoid inventing a number like 999 in the process of finding the lowest of a set of values, then my suggestion is to take the first value (of the set) as the lowest one, and then compare the second value to this first one, and so on.

The first value from the set will either be, coincidentally, the lowest value, or you will find lower values as you go through the collection. You will still end up finding the lowest value, just without having to invent some arbitrary number like 999.
Was This Post Helpful? 2
  • +
  • -

#5 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Trying to figure out how to get the value of the lowest price

Posted 20 February 2017 - 11:21 AM

Thanks for all the help, i got it up and running now
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1