-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.js
55 lines (45 loc) · 1.52 KB
/
calc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
$("#petrolprice").on('input keyup', update_discount_calcs);
$("#discount").on('input keyup', update_discount_calcs);
$("#cash").on('input keyup', update_discount_calcs);
function update_discount_calcs() {
var price = $("#petrolprice").val();
var discount = $("#discount").val();
var cash = $("#cash").val();
var litres = calc_litres(price, discount, cash);
var spend = calc_spend(price, litres);
if (isFinite(price)) {
$(".e10_price").html(calc_e10_breakpoint(price).toFixed(1));
}
if (isFinite(litres)) {
$(".result_litres").html(litres.toFixed(2));
var without_discount = calc_litres(price, 0, cash);
var petrol_gained = litres - without_discount;
if (isFinite(petrol_gained)) {
$(".petrol_gained").html(petrol_gained.toFixed(2));
}
}
if (isFinite(spend)) {
$(".result_spend").html(spend.toFixed(2));
}
}
function calc_litres(price, discount, cash) {
if (isNaN(price) || isNaN(discount) || isNaN(cash)) {
return 0
} else {
return (cash * 100) / (price - discount);
}
}
function calc_spend(price, litres) {
if (isNaN(price) || isNaN(litres)) {
return 0
} else {
return (price * litres) / 100;
}
}
function calc_e10_breakpoint(price) {
var petrol_kcal_litre = 7594.0;
var ethanol_kcal_litre = 5062.7;
var ratio = 0.10;
var e10_kcal_litre = petrol_kcal_litre * (1-ratio) + ethanol_kcal_litre * ratio;
return price * (e10_kcal_litre / petrol_kcal_litre);
}