Check value and run calculation in javascript - javascript

I am using javascript to calculate the tax of the products. I am using the following script for it:
<script>
function getTotalTax() {
var numVal1 = Number(document.getElementById("price1").value);
var numVal6 = Number(document.getElementById("tax1").value);
var numVal2 = Number(document.getElementById("price2").value);
var numVal7 = Number(document.getElementById("tax2").value);
var totalValue2 = (numVal1 / '100' * numVal6) + (numVal2 / '100' * numVal7);
document.getElementById("total_tax").value = totalValue2.toFixed(2);
}
</script>
Now I want to extend this script. I added three more textboxes. In the first textbox I want to add only the calculation for where the tax='10', in the second I want to add only where the tax='20' and in the third I want to add only where the tax='25'.
I need to make a script like this:
var totalValue3 = IF numval='10'(numVal1 / '100' * numVal6) + IF numval='10'(numVal2 / '100' * numVal7)
var totalValue3 = IF numval='20'(numVal1 / '100' * numVal6) + IF numval='20'(numVal2 / '100' * numVal7)
var totalValue3 = IF numval='25'(numVal1 / '100' * numVal6) + IF numval='25'(numVal2 / '100' * numVal7)
But this script is not working.
Does someone know how I can make a calculation for this?
JSfiddle:
https://jsfiddle.net/mm2fLne9/
Update:

I think this is the code you are looking for.
https://jsfiddle.net/mm2fLne9/8/
function getTotalTax() {
var numVal1 = Number(document.getElementById("price1").value);
var numVal6 = Number(document.getElementById("tax1").value);
var numVal2 = Number(document.getElementById("price2").value);
var numVal7 = Number(document.getElementById("tax2").value);
var totalValue2 = (numVal1 / '100' * numVal6) + (numVal2 / '100' * numVal7);
document.getElementById("total_tax").value = totalValue2.toFixed(2);
var totalValue3 = (numVal6 ==10 ?(numVal1 / 100 * numVal6) :0) + (numVal7 ==10 ?(numVal2 / 100 * numVal7):0);
document.getElementById("ttax1").value = totalValue3.toFixed(2);
var totalValue4 = (numVal6 ==20 ?(numVal1 / 100 * numVal6) :0) + (numVal7 ==20 ?(numVal2 / 100 * numVal7):0);
document.getElementById("ttax2").value = totalValue4.toFixed(2);
}

Related

How to modify a formula to include another element input inside my code?

On my pricing page, I've got a pricing estimator; input an order value and a return rate (%) and price estimate will print into a div block below. The formula should look like this: ((orderValue * 0.05, 2) + 4) + ((returnRate / 100) * 4)... Can anyone see why this isn't working?
var elDeliveryPrice = document.getElementById('deliveryPrice');
var elOrderValue = document.getElementById('orderValue');
var elReturnRate = document.getElementById('returnRate');
var formatter = new Intl.NumberFormat('gb-GB', { style: 'currency', currency: 'GBP' });
elOrderValue.addEventListener('keyup', _ => {
let curVal = elOrderValue.value;
let curValFloat = parseFloat(curVal);
if (isNaN(curValFloat)) {
elDeliveryPrice.innerHTML = '';
return;
}
elDeliveryPrice.innerHTML = formatter.format(Math.max(curValFloat * 0.05,2) + 4) + (parseInt("elReturnRate") / 100)) * 4;
});
There are a few things that cause your issues. For starters you should also listen to 'keyup' on the returnRate input box. Secondly you should handle NaN of the returnRate value.
For example:
var elDeliveryPrice = document.getElementById('deliveryPrice');
var elOrderValue = document.getElementById('orderValue');
var elReturnRate = document.getElementById('returnRate');
var formatter = new Intl.NumberFormat('gb-GB', { style: 'currency', currency: 'GBP' });
function calc() {
let curVal = elOrderValue.value;
let curValFloat = parseFloat(curVal);
let curRate = parseFloat(elReturnRate.value);
if (isNaN(curValFloat) || isNaN(curRate)) {
elDeliveryPrice.innerHTML = '£ _';
return;
}
elDeliveryPrice.innerHTML = formatter.format(Math.max(curValFloat * 0.05,2) + 4 + (curRate / 100) * 4);
}
elOrderValue.addEventListener('keyup', calc);
elReturnRate.addEventListener('keyup', calc);
You can divide a percent number by 100 to get the correct number to include in your calculation. If the return rate input is given in text, you can convert to a number using parseInt(returnRate) e.g.
Math.max(curValFloat * 0.05,2) + 4 + (parseInt(returnRate) / 100) * 4

How to calculate a total price JS

I have this code:
function calculate_sale_article(id){
var article_price = $("#article_price_"+id).val()
var article_cant = $("#article_cant_"+id).val()
var article_discount = $("#article_discount_"+id).val()
$("#article_ammount").val(article_price * article_cant / article_discount)
}
Error: "Infinity"
This error is when article_discount arrives with a 0, how can I fix this?
Add a simple check for zero (exception handling for divide by zero). Replace the below line:
$("#article_ammount").val(article_price * article_cant / article_discount)
With something like this:
if (article_discount != 0)
$("#article_ammount").val(article_price * article_cant / article_discount)
else
$("#article_ammount").val(article_price * article_cant)
var article_price = parseInt($("#article_price_"+id).val());
var article_cant = parseInt($("#article_cant_"+id).val());
var discount = parseInt($("#article_discount_"+id).val());
var article_discount = discount?discount:1;
$("#article_ammount").val(article_price * article_cant / article_discount)

What is the right way to add percentages to a service/product?

I have to add up the service charge(10%) plus VAT(8%) to a service, and I'm not sure the right way to increase a number by percentages.
For example: The service costs $1000 and I need to add to it 10% plus 8%. I'm struggling with three different ways to achieve that. Which way is the right one?
var service_charge = 10; // percent
var vat = 8; // percent
var price = 1000;
// OPTION 1
var tmp1 = price * (service_charge / 100);
var tmp2 = price * (vat / 100);
var total_1 = price + tmp1 + tmp2;
// OPTION 2
var tmp3 = price * (service_charge / 100);
var sub_total = price + tmp3;
var tmp4 = sub_total * (vat / 100);
var total_2 = sub_total + tmp4;
// OPTION 3
var tmp5 = price * ((service_charge + vat) / 100);
var total_3 = price + tmp5;
console.log(total_1);
console.log(total_2);
console.log(total_3);
https://jsbin.com/povama/edit?js,console
I think "option 2" is the right one, but I really want to be sure.
If VAT is to be applied after service charge, option 2 is the good one. If you want to summarize it in one single line it would be something like this:
var total = (price*(1+(service_charge/100)))*(1+(vat/100));

get array(s) from jquery / javascript function

I've got a javascript function which generates and returns a new array (of arrays):
function getFees(id){
var prep = new Array, primary = new Array, secondary = new Array, vce = new Array;
prep[0] = 733;
primary[0] = 792;
secondary[0] = 879;
vce[0] = 1108;
if (id == 2) {
prep[1] = (prep[0] - prep[0] * 5 / 100);
prep[1] = Math.ceil(prep[1]);
primary[1] = (primary[0] - primary[0] * 5 / 100);
primary[1] = Math.ceil(primary[1]);
secondary[1] = (secondary[0] - secondary[0] * 5 / 100);
secondary[1] = Math.floor(secondary[1]);
vce[1] = (vce[0] - vce[0] * 5 / 100);
vce[1] = Math.floor(vce[1]);
} else if (id == 3) {
prep[2] = (prep[0] - prep[0] * 10 / 100);
prep[2] = Math.ceil(prep[2]);
primary[2] = (primary[0] - primary[0] * 10 / 100);
primary[2] = Math.ceil(primary[2]);
secondary[2] = (secondary[0] - secondary[0] * 10 / 100);
secondary[2] = Math.floor(secondary[2]);
vce[2] = (vce[0] - vce[0] * 10 / 100);
vce[2] = Math.floor(vce[2]);
} else if (id == 4) {
prep[3] = (prep[0] - prep[0] * 50 / 100);
prep[3] = Math.ceil(prep[3]);
primary[3] = (primary[0] - primary[0] * 50 / 100);
primary[3] = Math.ceil(primary[3]);
secondary[3] = (secondary[0] - secondary[0] * 50 / 100);
secondary[3] = Math.ceil(secondary[3]);
vce[3] = (vce[0] - vce[0] * 50 / 100);
vce[3] = Math.floor(vce[3]);
} else if (id >= 5) {
prep[4] = (prep[0] - prep[0] * 75 / 100);
prep[4] = Math.floor(prep[4]);
primary[4] = (primary[0] - primary[0] * 75 / 100);
primary[4] = Math.ceil(primary[4]);
secondary[4] = (secondary[0] - secondary[0] * 75 / 100);
secondary[4] = Math.ceil(secondary[4]);
vce[4] = (vce[0] - vce[0] * 75 / 100);
vce[4] = Math.floor(vce[4]);
}
var newArray = [];
newArray.push({'prep':prep}); //prep array = 733,697
newArray.push({'primary':primary}); //primary array = 792,753
newArray.push({'secondary':secondary}); //secondary array = 879,835
newArray.push({'vce':vce}); //vce array = 1108,1052
return newArray;
}
Essentially I've given an example in the .push sections at the bottom. I then call my function by doing this:
var fees = getFees(2);
alert(fees);
Which alerts this:
[object Object],[object Object],[object Object],[object Object]
If I do:
alert(fees.toSource());
I get this:
[{prep:[733, 697]}, {primary:[792, 753]}, {secondary:[879, 835]}, {vce:[1108, 1052]}]
What I need to be able to do is get the number from any of the items (prep/primary/secondary/vce) eg..
fees.prep[0];
fees.primary[1];
But when I try that, I get this error:
TypeError: fees.prep is undefined
What am I missing?? Any help would be greatly appreciated!! :)
you need to access like this
fees[0].prep[0];

How do I convert 10 to .10 or 10% in javascript?

The third-party is feeding me 10 percent as the number 10.
In my code how do I move the decimal 2 places over for the variable itemDiscountPercent?
if (boolActive)
{
itemDiscountPercent = Math.percent(ogCookie.products[i].discount_percent);
itemPrice = ogCookie.products[i].price;
itemQty = ogCookie.products[i].quantity;
if (itemQty > 1)
{
itemDiscountPercent = itemDiscountPercent * itemQty;
}
priceDiscount = itemPrice * itemDiscountPercent;
alert(itemDiscountPercent);
alert(itemPrice);
alert(priceDiscount);
}
So instead of getting 299.8 for a line item with quantity 2, I need it to be 2.99 so I can subtract it later in the code.
divide by 100.
var dec = itemDiscountPercent / 100;
if (boolActive)
{
itemDiscountPercent = ogCookie.products[i].discount_percent;
itemPrice = ogCookie.products[i].price;
itemQty = ogCookie.products[i].quantity;
//priceDiscount = itemPrice * itemQty * itemDiscountPercent / 100;
priceDiscount = Math.round(itemPrice * itemQty * itemDiscountPercent - 0.5) / 100;
alert(itemDiscountPercent);
alert(itemPrice);
alert(priceDiscount);
}

Categories

Resources