Question: Hi i and to calculate the end date based on the user input and the start date 

```
<div class="form-group">
    <label>Months</label>
    <input type="text" name="chit_months" class="form-control">
</div>
<div class="form-group">
    <label>Start Date</label>
    <div class='input-group date' id='datetimepicker1'>
        <input type='text' class="form-control" name="start_date" id="example1" />
        <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar"></span>
        </span>
    </div>
</div>

<script>
    $('#example1').datepicker({
        format: "dd/mm/yyyy"
    });

    $('#example2').datepicker({
        format: "dd/mm/yyyy"
    });
</script>

```

I am using the datepicker, if the user enter the month in text box and he select the start date, The end date should automatically calculate by the jQuery 

Please help me to know how to do this.

Answer: 
Using the trivial mathematical relationship between consecutive indexes, you can write:

```
n1, n2 = hash.values_at(*existing_ranks)
are_neighbors = (n1 - n2).abs == 2 && hash[given_rank] == (n1 + n2) / 2

```
When a key is not found in a hash, then the result is `nil`.Just delete the `nil` to get the valid neighbor(s)

```
hash = {
  bronze: 0,
  silver: 1,
  gold: 2,
  platinum: 3,
  diamond: 4
}

given_rank = 'gold'
existing_ranks =  ['silver', 'platinum']

ranks = hash.invert
r = hash[given_rank.to_sym]
# compact method deletes nil values:
p existing_ranks.map(&:to_sym) == [ranks[r-1], ranks[r+1]].compact # => true

```