Skip to content

Instantly share code, notes, and snippets.

@joeriks
Created February 13, 2014 08:02
Show Gist options
  • Select an option

  • Save joeriks/8971459 to your computer and use it in GitHub Desktop.

Select an option

Save joeriks/8971459 to your computer and use it in GitHub Desktop.
Excel like calculations are horrible with jquery - time to move to knockout/angular
function flt(fieldName) {
var parsedValue = parseFloat($('#' + fieldName).val().replace(",", "."));
if (isNaN(parsedValue)) parsedValue = 0;
$('#' + fieldName).val(parsedValue);
return parsedValue;
}
function recalculate(reset) {
var r1 = (flt("i1") * 1000 - 2 * flt("i4"));
$("#r1").val(Math.round(r1));
$("#r2").text(flt("i2"));
$("#r3").text(flt("i3"));
var r4 = (flt("r1") - flt("i3")) / (flt("i2") + flt("i3"));
$("#r4").val(Math.round(r4 * 10) / 10);
$("#r5").val(Math.floor(r4));
if (reset) $("#i5").val(Math.floor(r4));
var r7 = r4 - flt("i5");
$("#r7").text(Math.round(r7 * 10) / 10);
var r8 = flt("i2") + flt("i3");
$("#r8").text(Math.round(r8));
$("#r8b").text(Math.round(r8));
var r6 = r7 * r8;
$("#r6").val(Math.round(r6));
var r9 = r6 / 2;
$("#r9").val(Math.round(r9 * 10) / 10);
var r10 = flt("i2") / 3;
$("#r13a").text(Math.round(r10));
$("#r13b").text(Math.round(r10));
$("#r14").text(Math.round(r1));
$("#r15").text(Math.round(r8));
if (r9 < r10) {
$("#r11").show();
$("#r12").hide();
$("#r9").addClass("error");
$("#r9").removeClass("strong");
} else if (r9 > flt("i2")) {
recalculate(true);
} else {
$("#r11").hide();
$("#r12").show();
$("#r9").addClass("strong");
$("#r9").removeClass("error");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment