Search code examples
jquerycalculatorjquery-calculation

Calculation (customers and servings)


This has been racking my brain all day, and I can't work out how to do it... can anyone help?

I want to use the jQuery Calculation plugin to perform a calculation from input boxes on my site...

For example:

Input boxes: Number of guests and Number of servings

100 or less guests: £180
+
£1 per each additional guest (101+)
+
£0.60 per each additional guest serving

Calculation: would be £180+(additional guests x £1)+(total guests x servings @ £0.60)

Example: 112 guests with 2 servings would be £180+£12+£67.20=£259.20

Can anyone help at all?

Thanks in advance


Solution

  • Final code:

    <script type="text/javascript">
    
    
    
    $(document).ready(function() {
    
    
    
    
    
    $('#numguests,#numservings').change(function() {
    var numguests = parseInt($('#numguests').val(), 10);
    var guestprice = 180;
    if (numguests > 100) {
    guestprice = guestprice + (numguests - 100);
    }
    var servingsprice = 0;
    var numservings = parseInt($('#numservings').val(), 10);
    if (numservings > 1) {
    servingsprice = numguests * 0.60 * (numservings-1);
    }
    $('#total').html(parseFloat(guestprice + servingsprice).toFixed(2));
    });
    
    });
    </script>
    
    
    <div id="calculator">
    <p><strong>Use our Cost Calculator to work out a price:</strong></p>
    <p>Number of guests <input id="numguests" value="100"><br>
    Number of 4oz servings per guest<input id="numservings" value="1"><br>
    Total price : &amp;<span id="total"></span></p>
    </div>
    
    
    
    
    </div>