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

Page 1 of 1

## 4 Replies - 1175 Views - Last Post: 20 February 2017 - 11:21 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=401491&amp;s=e38458df6e991bbc0795cfa4392de502&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jstanley6

Reputation: 1
• 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

• blow up my boots

Reputation: 6492
• Posts: 26,267
• 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

### #3 jstanley6

Reputation: 1
• 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?

### #4 andrewsw

• blow up my boots

Reputation: 6492
• Posts: 26,267
• 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.

### #5 jstanley6

Reputation: 1
• 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