Okay So I am trying to add tax into this equation if the dropdown for tax is selected as 1 or Yes My code below is what I have:
function Calculate() {
var par = $(this).parent().parent();
var price = $(par).find('input#price').val();
var tax = $(par).find('#tax').val();
if(price == ''){
return false;
}
var qty = $(this).val();
if(qty == ''){
return false;
}
if(tax == '1'){
var tax = "<?php echo $this->config->item('default_tax'); ?>";
}
else{
var tax = '0.00';
}
var total = parseFloat(price) * parseFloat(tax) * parseFloat(qty);
$(par).find('input#total').val(total.toFixed(2));
$(par).find('#Ltotal').text(total.toFixed(2));
CalculateTotal();
}
Before I added all the tax stuff, it was working just fine. But I need that tax figured in unfortunately.
Now when the above code runs I get the amount of tax on each product and not the total amount.
So for example if I have a tax rate as: 8.650%
And a line total of: $15.00
and a quantity of: 1
The script would return 1.30 for the amount. Which is the tax amount. The total should be 16.30
What am I doing wrong?
I'm going to say something is wrong with this line:
var total = parseFloat(price) * parseFloat(tax) * parseFloat(qty);
But not sure what.
The correct equation would be price * qty, then multiple times 1 + tax rate.
var total = (parseFloat(price) * parseFloat(qty)) * (1+parseFloat(tax));
You have to be careful though because javascript uses floats and this can cause some strange (unexact) measurements.
Related
I'm quite new to JavaScript so apologies in advanced, but I'm wanting too learn!
In my html form I am asking the customer for 3 numbers (number 1,2,3) and then in my JavaScript I am calculating the total of all of them. I need to work out my next bit of code so I can:
Given the total of the numbers give I can print out to the customer you're item is free (less than 180) or it has a cost (more than 180)
Given the number is under or over a certain amount return an error message
Where would be the best place to go with this ?
function Calculate() {
var number1 = document.getElementById("number1").value;
var number2 = document.getElementById("number2").value;
var number3 = document.getElementById("number3").value;
// var totalOfNumbers = Number(number1) + Number(number2) + Number(number3);
document.getElementById("total").innerHTML = Number(number1) + Number(number2) + Number(number3);
}
You need to use an if construct.
The code would be :-
function Calculate() {
var number1 = document.getElementById("number1").value;
var number2 = document.getElementById("number2").value;
var number3 = document.getElementById("number3").value;
// use parseInt instead of Number()
var totalOfNumbers =
parseInt(number1) + parseInt(number2) + parseInt(number3);
// string message to be displayed
var message = "";
// enter you own values
var minAmount = 0;
var maxAmount = 1000;
// checking if total of the numbers is under or over a certain amount;
// if it is, then showing an error message.
if (totalOfNumbers < minAmount || totalOfNumbers > maxAmount) {
message = "Error! Please enter valid amounts."
}
// Checking if total of the numbers is under 180;
// if it is, then displaying the item is free.
else if (totalOfNumbers < 180) {
message = "You're item is free!";
}
// Checking if total of the numbers is greater than or equal to 180;
// if it is, then displaying the item is not free, and its cost.
else {
message = "You're item is not free. It has a cost of $" + totalOfNumbers;
}
document.getElementById("total").innerHTML = message;
}
I have this exercise that I am struggling to comprehend the logic to achieve the outcome when qty is more than 3:
An application to calculate the price of pizzas (listed below) that will be purchased during a promotional period. The number of pizzas will be entered by the user.
One large pizza will cost $6.45.
Two large pizzas will cost $12.00.
Three large pizzas will cost $14.00.
Four or more pizzas will use a combination of the above prices to ensure the best price for the customer. For example, the best price for five pizzas would be two pizzas ($12.00) + three pizzas ($14.00).
The algorithm must also take account of all possible situations by using sequence, selection and iteration structures.
Below is my code so far:
let calcOrder = () => {
// get inputs
var qty = document.getElementById("quantity").value;
var price = 6.45;
var price2 = 12.0;
var price3 = 14.0;
var totalPrice;
// validate missing, non-digit, negative inputs
if (qty == "") {
document.getElementById("message").innerHTML = "Missing input";
} else if (isNaN(qty)) {
document.getElementById("message").innerHTML = "Numbers only";
} else if (qty < 0) {
document.getElementById("message").innerHTML =
"Negative numbers are not allowed";
} else {
//calc total
if (qty == 1)
{
totalPrice = price;
}
else if (qty == 2)
{
totalPrice = price2;
}
else if (qty == 3)
{
totalPrice = price3;
}
//output total
document.getElementById("message").innerHTML =
`Total price is $${totalPrice}`;
}
// prevent form from submission
return false;
};
Thank you
you can use division and mod operation to calculate the price:
(this example assuming all check you did valid input are already done)
const qte = document.getElementById("quantity").value;
const price = 6.45;
const price2 = 12.0;
const price3 = 14.0;
let totalPrice;
const total3 = parseInt("" + qte / 3 + "", 10) * price3;
const total2 = parseInt("" + (qte % 3) / 2 + "", 10) * price2;
const total1 = parseInt("" + ((qte % 3) % 2) + "", 10) * price;
totalPrice = total1 + total2 + total3;
document.getElementById("message").innerHTML =
`Total price is $${totalPrice}`;
what is actually happening in the code
well it is basic mathematics, if you want to know how many 3s in your number you divide by 3 and take the integer part or floor of the result, basic division. for example 10 = 3*3 + 1 so you have 3 as a result of that division.
since you only want to apply the price of 3 pizza as top priority you do this division then multiply by the price for 3.
then come the priority for the price of 2 pizzas, but you not interested for the total number of pizzas, only what was left after you payed with the price of 3 pizzas so you do the mod operator (%) with 3 to get was left unpaid, for example 8 = 3*2 + 2, this give us 2 pizzas left unpaid so you apply the price of 2 pizzas.
the you check if a single pizza is left after you paid for 2 pizzas (which would only happen if only a single pizza was left after you paid for 3). if there is single pizza you pay for it otherwise you add nothing.
ps: after paying for pizzas in multiple of three, you only add the price of 2 pizzas or a single one, but never both, otherwise price of 3 pizzas would apply instead.
hope the explanation is clear, if not leave a comment and i'll try to adjust what was not clear.
My JavaScript is:
$('tbody').delegate('.qty,.price,.dis', 'keyup', function() {
var tr = $(this).parent().parent();
var qty = tr.find('.qty').val();
var price = tr.find('.price').val();
var dis = tr.find('.dis').val();
var amount = (qty * price) - (qty * price * dis) / 100;
tr.find('.amount').val(amount);
total();
var tax = tr.find('.tax').val();
var g_total = total() + (total() * tax) / 100;
tr.find('.g_total').val(g_total);
});
Now I want the user to be able to add sales tax and automatically give the grand total to the user. My JavaScript is working correctly until total but the rest not working.
I have a script that calculates totals based on price, quantity, tax and shipping cost. When I attempt to add them together I receiving high numbers for total cost
Here is the script:
function calculate() {
var total = 0;
var shiptotal = 0;
var subtotal = 0;
var taxtotal = 0;
var taxrate = .078;
$('.button-click').each(function () {
var amt = parseInt($(this).prev().val());
var qty = parseInt($(this).parent().find(".quantity").val());
var ship = parseInt($(this).parent().find(".ik-ship").val());
shiptotal += (ship * qty);
subtotal += (amt * qty);
taxtotal += ( (amt * qty) * taxrate);
total += ( subtotal + shiptotal + taxtotal );
});
$('#Amount').val(total.toFixed(2));
$('.total-amount').html( total.toFixed(2) );
$('.sub-total-amount').html( subtotal.toFixed(2) );
$('.shipping-amount').html( shiptotal.toFixed(2) );
$('.tax-amount').html( taxtotal.toFixed(2) );
}
A product that cost $62.00 and has $3.00 in shipping comes out like this:
SUB-TOTAL: 62.00
SHIPPING: 3.00
TAX: 4.84
TOTAL: 1257.05 <-- incorrect total -->
I may have been in front of the computer for too long but how do I solve this problem? Please provide an example.
Use integer math by multiplying the numbers for 1000.
I have the following code:
function calculateVat() {
var vat = null;var amount=null;
vat = document.getElementById("hiddenvat").value;
amount = document.getElementById("numtxt_itemCost").value;
var total = parseInt(amount)* parseInt(vat) / 100 ;
document.getElementById("txt_Total_text").value = total;
}
Here I am calculating VAT price according to amount price
E.G. VAT value is 13.5
If I input 1780 on numtxt_itemCost Field then it should give the output 240.30,
but when I run this programme by putting value 1780 on numtxt_itemCost field
it shows the 231.4 which is the wrong output,
You should use parseFloat instead:
parseFloat(1780)* parseFloat(13.5) / 100 //240.3
when you use parseInt, all decimals get stripped off.
Try this:
function calculateVat() {
var vat = null;var amount=null;
vat = document.getElementById("hiddenvat").value;
amount = document.getElementById("numtxt_itemCost").value;
var total = parseInt(amount)* parseFloat(vat) / 100 ;
document.getElementById("txt_Total_text").value = total;
}
The problem is that you were converting the vat to an "int" using parseInt(vat), so the answer you were seeing was the result of a 13% vat rather than 13.5%.