javascript - How do you call a specific value from inside a math function? -


in code add number of values based on form , subtract lowest value creates new total. code works great. instead of having new total @ bottom, 3. 1 says original total without subtraction, 1 value of lowest price subtract, , actual new total. of displaying new total.

example: there 3 items scarf $5, hat $10, , jacket $20. add values equals $35, subtract lowest value scarf $5 , new total of $30. want display @ bottom.

total: $35 discount: $-5 new total: $30

the function figures out these values, don't know how call them function.

// selected prices stored on array var prices = [];  // function remove item array function remove(array, item) {     (var = 0, len = array.length; < len; i++) {         if (array[i] == item) {             array.splice(i, 1);             return true;         }     }     return false; }  function calculatesecteddues(checkbox, amount) {     // add or remove price necesary  if (checkbox.checked) {     prices.push(amount); } else {     remove(prices, amount); }  // sum prices var total = 0; (var = 0, len = prices.length; < len; i++)     total += prices[i];  // lower 1 var min = math.min.apply(math, prices);   if(min == infinity) { min = 0; };  // , substract total -= min;  document.grad_enroll_form.total.value = total; 

}

here basic code writes out values. still not agree looping removing elements array.

<span id="value"></span> 
var prices = [];  function remove(arr,itm){     var indx = arr.indexof(itm);     if (indx !== -1){         arr.splice(indx,1);     } }  function calculatesecteddues(checkbox, amount) {     if (checkbox.checked === true) {         prices.push(amount);     } else {         remove(prices, amount);     }      var total = 0;     (var = 0, len = prices.length; < len; i++)         total += prices[i];      var min = prices.slice().sort(function(a,b){return a-b})[0];     if(typeof min === 'undefined') min = 0;      var withdiscount = total - min;     var discountamount = withdiscount - total;      document.queryselector("#value").innerhtml = "total: $"+total+'<br>';     document.queryselector("#value").innerhtml += "discount: $"+discountamount+'<br>';     document.queryselector("#value").innerhtml += "new total: $"+withdiscount+'<br>'; } 

run this:

calculatesecteddues({checked:true},10); calculatesecteddues({checked:true},20); calculatesecteddues({checked:true},5); 

gives output:

total: $35
discount: $-5
new total: $30

jsfiddle


using checkboxes

<label><input type="checkbox" onclick="calculatesecteddues(this,5)" name="scarf"> <span>scarf</span><label><br> <label><input type="checkbox" onclick="calculatesecteddues(this,10)" name="hat"> <span>hat</span><label><br> <label><input type="checkbox" onclick="calculatesecteddues(this,20)" name="jacket"> <span>jacket</span><label><br>  <span id="value">total: $0<br>discount: $0<br>new total: $0</span> 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -