Round off Issue with JavaScript - javascript

I am making a small project where a Javascript is supposed to perform price, dicsount and tax calculation. Expected calcualtion should be -
(Unit Price - Discount) + TAX
Discount could be in Percent or number.
The problem I am facing is with round off.
For Example -
Unit Price = 3.95
Discount = 5%
TAX = 28%
My javascript gives output value of 4.82 after round off.
When same thing is calculated using calculator it is showing 4.8032
It is a huge difference.
What am I doing wrong ?
My Javascript code is as follows -
function calculate_total(value) {
var id = document.getElementById('id').value;
var tot_qty=0;
var tot_vat=0;
var tot_sat=0;
var tot_disc=0;
var tot_taxable_amount = 0;
var taxable_amount = 0;
var total_amount=0;
var total_amount1=0;
var grand_total=0;
var overall_total=0;
for(i=1;i<=id;i++){
var up = document.getElementById('part_desc'+i+'_unitprice').value;
var disc = document.getElementById('part_desc'+i+'_discount').value;
up = parseFloat(up);
if(!up){
up = 0;
}
if(disc.search('%')==-1){
disc = parseFloat(disc);
var disc_symbol=0;
}
else{
disc = disc.replace('%','');
disc = parseFloat(disc);
var disc_symbol=1;
}
if(!disc){
disc=0;
}
if(disc_symbol==1){
disc = (up*disc/100);
var price = up - disc;
}
else{
var price = up-disc;
}
taxable_amount = Math.round(price*100)/100;
tot_taxable_amount += taxable_amount;
var vat = $('.part_desc'+i+'_cgst').html();
vat = vat.replace("%","");
var sat = $('.part_desc'+i+'_sgst').html();
sat = sat.replace("%","");
vat = parseFloat(vat);
//alert(vat+'-'+sat);
if(!vat){
vat = 0;
}
sat = parseFloat(sat);
if(!sat){
sat = 0;
}
vat = (Math.round((price*vat/100)*100))/100;
sat = (Math.round((price*sat/100)*100))/100;
$('.part_desc'+i+'_cgst_value').html(vat);
$('.part_desc'+i+'_cgst_value_hidden').val(vat);
$('.part_desc'+i+'_cgst_hidden').val(vat);
$('.part_desc'+i+'_sgst_value').html(sat);
$('.part_desc'+i+'_sgst_value_hidden').val(vat);
$('.part_desc'+i+'_sgst_hidden').val(vat);
price = price + vat + sat;
price = Math.round(price*100)/100;
var qty = document.getElementById('part_desc'+i+'_quantity').value;
qty = parseFloat(qty);
if(!qty){
qty = 0;
}
var total = qty * price;
total = Math.round(total*100)/100;
vat = vat*qty;
sat = sat*qty;
disc = disc*qty;
tot_qty+=qty;
tot_vat+=vat;
tot_sat+=sat;
tot_disc+=disc;
total_amount+=total;
$('.part_desc'+i+'_taxable_price').html(taxable_amount);
taxable_amount = taxable_amount*qty;
$('.part_desc'+i+'_taxable').html(taxable_amount);
$('.part_desc'+i+'_taxable_hidden').val(taxable_amount);
document.getElementById('part_desc'+i+'_eprice').value = price;
$('.part_desc'+i+'_total').html(total);
$('.part_desc'+i+'_total_hidden').val(total);
}
tot_disc = Math.round(tot_disc*100)/100;
tot_taxable_amount = Math.round(tot_taxable_amount*100)/100;
tot_vat = Math.round(tot_vat*100)/100;
tot_sat = Math.round(tot_sat*100)/100;
total_amount = Math.round(total_amount*100)/100;
$('#tot_qty').html(tot_qty);
$('#tot_disc').html(tot_disc);
$('#tot_taxable').html(tot_taxable_amount);
$('#total_cgst').html(tot_vat);
$('#total_sgst').html(tot_sat);
$('#total_amount').html(total_amount);
$('#tot_qty_hidden').val(tot_qty);
$('#tot_disc_hidden').val(tot_disc);
$('#tot_taxable_hidden').val(tot_taxable_amount);
$('#total_cgst_hidden').val(tot_vat);
$('#total_sgst_hidden').val(tot_sat);
$('#total_amount_hidden').val(total_amount);
total_amount1 = total_amount;
var labor = document.getElementById('labor').value;
var transport_charge = document.getElementById('transport_charge').value;
var expenses = 0;
labor = parseFloat(labor);
if(!labor){
labor=0;
}
transport_charge = parseFloat(transport_charge);
if(!transport_charge){
transport_charge=0;
}
expenses = labor + transport_charge;
document.getElementById('total_expenses').value = expenses.toFixed(2);
total_amount1 = total_amount1+expenses;
var round_off = total_amount1;
dummy_grand_total = total_amount1.toFixed(2);
round_off = round_off.toFixed(0) - dummy_grand_total;
if(document.getElementById('round_off_manual').checked==false){
document.getElementById('round_off').value = round_off.toFixed(2)
total_amount1 = total_amount1.toFixed(0);
}
else{
var round_off_value = document.getElementById('round_off').value;
round_off_value = parseFloat(round_off_value);
if(!round_off_value){
round_off_value=0;
}
total_amount1 = parseFloat(total_amount1.toFixed(2))+round_off_value;
total_amount1 = total_amount1.toFixed(2);
}
$('#total_amount1').val(total_amount1);
var other_disc = document.getElementById('other_discount').value;
if(other_disc.search('%')==-1){
other_disc = parseFloat(other_disc);
var disc_symbol=0;
}
else{
other_disc = other_disc.replace('%','');
other_disc = parseFloat(other_disc);
var disc_symbol=1;
}
if(!other_disc){
other_disc=0;
}
if(disc_symbol==1){
other_disc = (total_amount1*other_disc/100);
grand_total = total_amount1 - other_disc;
}
else{
grand_total = total_amount1-other_disc;
}
var round_off = grand_total;
dummy_grand_total = grand_total.toFixed(2);
round_off = round_off.toFixed(0) - dummy_grand_total;
if(document.getElementById('round_off_manual').checked==false){
document.getElementById('round_off_dn').value = round_off.toFixed(2)
grand_total = grand_total.toFixed(0);
}
else{
var round_off_value = document.getElementById('round_off_dn').value;
round_off_value = parseFloat(round_off_value);
if(!round_off_value){
round_off_value=0;
}
grand_total = parseFloat(total_amount1.toFixed(2))+round_off_value;
grand_total = grand_total.toFixed(2);
}
document.getElementById('grand_total').value = grand_total;
}

Thanks to #JDHrnnts
The answer to the problem is -
Save the computed data to a variable then display with Math.round, but do not modify the variable.

Related

How to check multiple arrays at once?

I have this code. It checks if auctions[0] exists and if it does, it gets the value and sends it and continues to the next number. If not it will move on to the next number and do the same thing. And I need it to check if it exists until it reaches number 30
Are there any alternatives to this that is less bulky and messy?
if (ahValue.auctions[0]){
var itemName = ahValue.auctions[0].item_name
var itemLore = ahValue.auctions[0].item_lore
var itemTier = ahValue.auctions[0].tier
var itemSeller = ahValue.auctions[0].auctioneer
var itemBids = ahValue.auctions[0].bids.length
console.log(`${itemName}${itemLore}${itemTier}${itemSeller}${itemBids}`)
}
if (ahValue.auctions[1]){
var itemName = ahValue.auctions[1].item_name
var itemLore = ahValue.auctions[1].item_lore
var itemTier = ahValue.auctions[1].tier
var itemSeller = ahValue.auctions[1].auctioneer
var itemBids = ahValue.auctions[1].bids.length
console.log(`${itemName}${itemLore}${itemTier}${itemSeller}${itemBids}`)
}
//copy and paste until it reaches 30
if (ahValue.auctions[30]){
var itemName = ahValue.auctions[30].item_name
var itemLore = ahValue.auctions[30].item_lore
var itemTier = ahValue.auctions[30].tier
var itemSeller = ahValue.auctions[30].auctioneer
var itemBids = ahValue.auctions[30].bids.length
console.log(`${itemName}${itemLore}${itemTier}${itemSeller}${itemBids}`)
}
Use a for loop:
for(let i = 0; i <= 30; i++) {
if (ahValue.auctions[i]){
var itemName = ahValue.auctions[i].item_name
var itemLore = ahValue.auctions[i].item_lore
var itemTier = ahValue.auctions[i].tier
var itemSeller = ahValue.auctions[i].auctioneer
var itemBids = ahValue.auctions[i].bids.length
console.log(`${itemName}${itemLore}${itemTier}${itemSeller}${itemBids}`)
}
}
try something like this.
auctions.length this will return the lenght of array, and this will help you to iterate over all the elements.
This is the running code snippet.
var auctions = [];
auctions[0] = { "item_name" : "item_name1", "item_lore" : "item_lore1", "tier" : "tier1", "auctioneer" : "auctioneer1" , "bids":"bids1"};
auctions[1] = { "item_name" : "item_name2", "item_lore" : "item_lore2", "tier" : "tier2", "auctioneer" : "auctioneer2" , "bids":"bids2"};
for(var i = 0; i < auctions.length; i++){
if(auctions[i]){
console.log(auctions[i].item_name);
}
}
And your full code will look like this:
for(let i = 0; i <= ahValue.auctions.length; i++) {
if (ahValue.auctions[i]){
var itemName = ahValue.auctions[i].item_name
var itemLore = ahValue.auctions[i].item_lore
var itemTier = ahValue.auctions[i].tier
var itemSeller = ahValue.auctions[i].auctioneer
var itemBids = ahValue.auctions[i].bids.length
console.log(`${itemName}${itemLore}${itemTier}${itemSeller}${itemBids}`)
}
}

Karatsuba Algorithm implemented in JavaScript not accurate

I am trying to implement the Karatsuba Algorithm in JavaScript. Below is my Implementation:
var bigInt = require("big-integer");
function bigKaratsuba(num1, num2) {
if (Number(num1) < 10 || Number(num2) < 10) {
return (Number(num1)*Number(num2)).toString();
}
var len1 = String(num1).length;
var len2 = String(num2).length;
var m = Math.max(len1, len2);
var m2 = Math.floor(m/2);
var high1 = String(num1).substring(0, m-m2);
var low1 = String(num1).substring(m-m2, len1);
var high2 = String(num2).substring(0, m-m2);
var low2 = String(num2).substring(m-m2, len2);
var high1 = bigInt(String(num1).substring(0, m-m2));
var low1 = bigInt(String(num1).substring(m-m2, len1));
var high2 = bigInt(String(num2).substring(0, m-m2));
var low2 = bigInt(String(num2).substring(m-m2, len2));
var low1AndHigh1 = low1.add(high1).toString();
var low2AndHigh2 = low2.add(high2).toString();
var high1 = String(high1);
var low1 = String(low1);
var high2 = String(high2);
var low2 = String(low2);
var z0 = bigKaratsuba(low1, low2);
var z1 = bigKaratsuba(low1AndHigh1, low2AndHigh2);
var z2 = bigKaratsuba(high1, high2);
var z0_int = bigInt(z0);
var z1_int = bigInt(z1);
var z2_int = bigInt(z2);
var product = z2_int.times(Math.pow(10, m2*2)).add(z1_int.minus(z2_int).minus(z0_int).times(Math.pow(10, m2))).add(z0_int);
return String(product);
}
bigKaratsuba(15, 15);
<script src="http://peterolson.github.com/BigInteger.js/BigInteger.min.js"></script>
This implementation works perfectly for small value, however while I am trying to calculate
3141592653589793238462643383279502884197169399375105820974944592 times
2718281828459045235360287471352662497757247093699959574966967627, the answer gose wrong, what I got is
8541020071716445382689180042569347598344699394502900911882868254737925119641226981222291225174629534283004908305569988292269254, which is not correct. The correct answer is 8539734222673567065463550869546574495034888535765114961879601127067743044893204848617875072216249073013374895871952806582723184 which I got it from here.
I have tried search on the stackoverflow but I haven't find any solution to my problem. There is no error or anything when I run my code. Any help or pointer will be highly appreciated. Thanks in advance for all the people who are being helpful.
With the great help of #Spektre and #greybeard, I managed to solve this problem. Below is the correct code:
var bigInt = require("big-integer");
function bigKaratsuba(num1, num2) {
if (Number(num1) < 10 || Number(num2) < 10) {
return (Number(num1)*Number(num2)).toString();
}
var len1 = String(num1).length;
var len2 = String(num2).length;
var m = Math.max(len1, len2);
var m2 = Math.floor(m/2);
var high1 = bigInt(String(num1).substring(0, len1-m2));
var low1 = bigInt(String(num1).substring(len1-m2, len1));
var high2 = bigInt(String(num2).substring(0, len2-m2));
var low2 = bigInt(String(num2).substring(len2-m2, len2));
var low1AndHigh1 = low1.add(high1).toString();
var low2AndHigh2 = low2.add(high2).toString();
var high1 = String(high1);
var low1 = String(low1);
var high2 = String(high2);
var low2 = String(low2);
var z0 = bigKaratsuba(low1, low2);
var z1 = bigKaratsuba(low1AndHigh1, low2AndHigh2);
var z2 = bigKaratsuba(high1, high2);
var z0_int = bigInt(z0);
var z1_int = bigInt(z1);
var z2_int = bigInt(z2);
var z1MinusZ2MinusZ0 = z1_int.minus(z2_int).minus(z0_int).toString();
var product = bigInt(addTrailingZero(z2, m2*2)).add(bigInt(addTrailingZero(z1MinusZ2MinusZ0, m2))).add(z0);
return String(product);
}
function addTrailingZero (numericString, numberOfZeroAdded) {
for (var i = 0; i < numberOfZeroAdded; i++) {
numericString = numericString + "0";
}
return numericString;
}
<script src="http://peterolson.github.com/BigInteger.js/BigInteger.min.js"></script>
As #Spektre pointed out, my original solution does truncate the mantissa since the biggest safe integer is 2^53-1. So one should add zero instead of doing the multiplication, besides it's also faster to add zero I believe.(Let me know if I am wrong).
Besides after trying the number suggested by #greybeard, I found that my original code snippet doesn't provide the correct solution so I changed it to
var high1 = bigInt(String(num1).substring(0, len1-m2));
var low1 = bigInt(String(num1).substring(len1-m2, len1));
var high2 = bigInt(String(num2).substring(0, len2-m2));
var low2 = bigInt(String(num2).substring(len2-m2, len2));

Javascript: window.confirmation

I have a function:
function placeOrder(price, productList) {
var bulletinBoardItem = Number(productList.box1.value);
var stickersItem = Number(productList.box2.value);
var cutoutsItem = Number(productList.box3.value);
var trimmerItem = Number(productList.box4.value);
var resourceBooksItem = Number(productList.box5.value);
var price = new Array(5);
price[0] = 12;
price[1] = 1;
price[2] = 6;
price[3] = 3;
price[4] = 20;
var sumBB = bulletinBoardItem * price[0];
var sumStickers = stickersItem * price[1];
var sumCutouts = cutoutsItem * price[2];
var sumTrimmer = trimmerItem * price[3];
var sumRB = resourceBooksItem * price[4];
}
and I have a window.confirm box that I need to reference the above function from, but none of the variables are recognized in that function. I don't understand what I'm doing wrong here.
<input type="button" onClick="placeOrder()" value="Place Order">
<p id = "order"></p>
<script>
function placeOrder(quantity, price, productList) {
var r = confirm("You've ordered Bulletin Boards");
if (r) {
window.alert("Your order has been placed!!!")
} else {
window.alert("Your order has been canceled.");
}
document.getElementById("order").innerHTML = txt;
}
</script>
You are using txt which is not defined :
document.getElementById("order").innerHTML = txt;
Thus, try to change txt by any defined string and your script will not be failed

Javascript doesn't generate email

I've coded some js into an Articulate Storyline file for a workshop. I'm trying to get the system to compile a number of variables and email the data to the user. When I click on the icon to generate the email I get nothing.
SET DATE CODE
var d = new Date();
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthName = new Array("January","February","March","April","May","June","July","August","Septemeber","October","November","December");
var dateVal = d.getDate().toString();
var delimeter = ", ";
var delimeter1 = " ";
var txtPostdate = ""
var lDigit = dateVal.charAt( dateVal.length-1);
if (lDigit = = "1"){
txtPostdate = "st";
}else if(lDigit = = "2"){
txtPostdate = "nd";
}else if(lDigit = = "3"){
txtPostdate = "rd";
}else{
txtPostdate = "th";
}
var dateString = weekday[d.getDay()]+delimeter+monthName[(d.getMonth())]+delimeter1+dateVal+txtPostdate+delimeter+d.getFullYear();
var player = GetPlayer();
player.SetVar("SystemDate",dateString);
SEND EMAIL CODE
var player = GetPlayer();
var useremail = player.GetVar("email");
var subject ="My Fact Sheet - Optimizing Your Online Job Applications";
var firstname = player.GetVar("firstname");
var lastname = player.GetVar("lastname");
var street = player.GetVar("street1");
var address2 = player.GetVar("street2");
var city = player.GetVar("city");
var state = player.GetVar("state");
var zip = player.GetVar("zip");
var phone = player.GetVar("phone");
var cell = player.GetVar("cell");
var mymail = player.GetVar("mymail");
var phone = player.GetVar("phone");
var cell = player.GetVar("cell");
var school1 = player.GetVar("school1");
var s1city = player.GetVar("s1city");
var s1degree = player.GetVar("s1degree");
var s1date = player.GetVar("s1date");
var s1start = player.GetVar("s1start");
var school2 = player.GetVar("school2");
var s2city = player.GetVar("s2city");
var s2degree = player.GetVar("s2degree");
var s2start = player.GetVar("s2start");
var s2date = player.GetVar("s2date");
var school3 = player.GetVar("school3");
var s3city = player.GetVar("s3city");
var s3degree = player.GetVar("s3degree");
var s3date = player.GetVar("s3date");
var s3start = player.GetVar("s3start");
var e1title = player.GetVar("e1title");
var e1start = player.GetVar("e1start");
var e1end = player.GetVar("e1end");
var e1 = player.GetVar("e1");
var e1employer = player.GetVar("e1employer");
var e1city = player.GetVar("e1city");
var e1state = player.GetVar("e1state");
var e1zip = player.GetVar("e1zip");
var e1super = player.GetVar("e1super");
var e1phone = player.GetVar("e1phone");
var e1reason = player.GetVar("e1reason");
var e1startsalary = player.GetVar("e1startsalary");
var e1endsalary = player.GetVar("e1endsalary");
var e1accomplish = player.GetVar("e1accomplish");
var e2title = player.GetVar("e2title");
var e2start = player.GetVar("e2start");
var e2end = player.GetVar("e2end");
var e2 = player.GetVar("e2");
var e2employer = player.GetVar("e2employer");
var e2city = player.GetVar("e2city");
var e2state = player.GetVar("e2state");
var e2zip = player.GetVar("e2zip");
var e2super = player.GetVar("e2super");
var e2phone = player.GetVar("e2phone");
var e2reason = player.GetVar("e2reason");
var e2startsalary = player.GetVar("e2startsalary");
var e2endsalary = player.GetVar("e2endsalary");
var e2accomplish = player.GetVar("e2accomplish");
var e3title = player.GetVar("e3title");
var e3start = player.GetVar("e3start");
var e3end = player.GetVar("e3end");
var e3 = player.GetVar("e3");
var e3employer = player.GetVar("e3employer");
var e3city = player.GetVar("e3city");
var e3state = player.GetVar("e3state");
var e3zip = player.GetVar("e3zip");
var e3super = player.GetVar("e3super");
var e3phone = player.GetVar("e3phone");
var e3reason = player.GetVar("e3reason");
var e3startsalary = player.GetVar("e3startsalary");
var e3endsalary = player.GetVar("e3endsalary");
var e3accomplish = player.GetVar("e3accomplish");
var r1name = player.GetVar("r1name");
var r1company = player.GetVar("r1company");
var r1email = player.GetVar("r1email");
var r1phone = player.GetVar("r1phone");
var r2name = player.GetVar("r2name");
var r2company = player.GetVar("r2company");
var r2email = player.GetVar("r2email");
var r2phone = player.GetVar("r2phone");
var r3name = player.GetVar("r3name");
var r3company = player.GetVar("r3company");
var r3email = player.GetVar("r3email");
var r3phone = player.GetVar("r3phone");
var cover = player.GetVar("cover");
var resume = player.GetVar("resume");
var mail = player.GetVar("mail");
var mailto_link='mailto:'+useremail+'?subject='+subject+'&body='+"Activity Notes – Optimizing Your Online Job Applications %0d%0A %0d%0AMy Name:%0d%0A”+firstname+”%0d%0A”+lastname+”%0d%0A%0d%0AMy Address:%0d%0A”+street1+”%0d%0A”+street2+”%0d%0A”+city+”%0d%0A”+state+”%0d%0A”+zip+”%0d%0A%0d%0AMy Home Phone: %0d%0A"+phone+”%0d%0A%0d%0AMy Cell Phone: %0d%0A”+cell+”%0d%0A%0d%0AMy eMail: %0d%0A "+mymail+”%0d%0A%0d%0AMy Education:%0d%0A”+school1+”%0d%0A”+s1city+”+s1degree+”%0d%0A”+s1start+”%0d%0A”+s1date+”%0d%0A%0d%0A”+school2+”%0d%0A”+s2city+”%0d%0A”+s2degree+”%0d%0A“+s1start+”%0d%0A”+s2date+”%0d%0A%0d%0A”+school3+”%0d%0A“+s3city+”%0d%0A“+s3degree+”%0d%0A“+s1start+”%0d%0A“+s3date+”%0d%0A%0d%0AMy Employment History - Job 1: %0d%0A“+e1title+”%0d%0A%0d%0ADates: %0d%0A “+e1start+”%0d%0A“+e1end+”%0d%0A%0d%0AEmployer Information:%0d%0A“+e1+”%0d%0A“+e1employer+”%0d%0A“+e1city+”%0d%0A“+e1state+”%0d%0A“+e1zip+”%0d%0A%0d%0ACompany Contact: %0d%0A“+e1super+”%0d%0A“+e1phone+”%0d%0A%0d%0AReason for Leaving: %0d%0A“+e1reason+”%0d%0A%0d%0AStarting Salary: “+e1startsalary+”%0d%0AEnding Salary: ”+e1endsalary+”%0d%0A%0d%0AAccomplishments:%0d%0A“+e1accomplish+”%0d%0A%0d%0AMy Employment History - Job 2:%0d%0A“+e2title+”%0d%0A%0d%0ADates:%0d%0A“+e2start+”%0d%0A“+e2end+”%0d%0A%0d%0AEmployer Information: %0d%0A“+e2+”%0d%0A“+e2employer+”%0d%0A“+e2city+”%0d%0A“+e2state+”%0d%0A“+e2zip+”%0d%0A%0d%0ACompany Contact: %0d%0A“+e2super+”%0d%0A“+e2phone+”%0d%0A%0d%0AReason for Leaving:%0d%0A“+e2reason+”%0d%0A%0d%0AStarting Salary: “+e2startsalary+”%0d%0AEnding Salary: “+e2endsalary+”%0d%0A%0d%0AAccomplishments:%0d%0A“+e2accomplish+”%0d%0A%0d%0AMy Employment History - Job 3:%0d%0A“+e3title+”%0d%0A%0d%0ADates:%0d%0A“+e3start+”%0d%0A“+e3end+”%0d%0A%0d%0AEmployer Information:%0d%0A
“+e3+”%0d%0A“+e3employer+”%0d%0A“+e3city+”%0d%0A“+e3state+”%0d%0A“+e3zip+”%0d%0A%0d%0ACompany Contact:%0d%0A“+e3super+”%0d%0A“+e3phone+”%0d%0A%0d%0AReason for Leaving:%0d%0A“+e3reason+”%0d%0A%0d%0AStarting Salary: “+e3startsalary+”%0d%0AEnding Salary: “+e3endsalary+”%0d%0A%0d%0AAccomplishments:%0d%0A“+e3accomplish+”%0d%0A%0d%0AMy References:%0d%0A“+r1name+”%0d%0A“+r1company+”%0d%0A“+r1email+”%0d%0A“+r1phone+”%0d%0A%0d%0A“+r2name+”%0d%0A“+r2company+”%0d%0A“+r2email+”%0d%0A“+r2phone+”%0d%0A%0d%0A“+r3name+”%0d%0A“+r3company+”%0d%0A“+r3email+”%0d%0A“+r3phone+”%0d%0A%0d%0AMy Resume:%0d%0A“+resume+”%0d%0A%0d%0AMy Cover Letter:%0d%0A“+cover+”%0d%0A%0d%0A
win=window.open(mailto_link,'emailWin');
Try putting %20 in place of spaces. Text in the submitted URL has spaces "Activity Notes – Optimizing Your Online.........". I did not survey the entire URL, but be sure to encode wherever needed.

JS script to calculates sum of amount calculates wrong

I have prepared this jsfiddle that ilustrates how my script calculate twice the sum of each selected option attribute price. Please help me to solve this issue.
optionsamount is wrong, I mean is calculated twice.. Why is that? Thanks
function update_amounts(){
var sum = 0.0;
var optionsamount = 0.0;
$('#basketorder > tbody > .product').each(function() {
$('.selectedoptionselect option:selected').each(function(){
optprice = $(this).attr('price');
optionsamount+= parseFloat(optprice);
})
var qty = $(this).find('.qty option:selected').val();
var price = $(this).find('.price').val();
var amount = (qty*price);
sum+= (amount + optionsamount);
$(this).find('.amount').text(''+ amount.toFixed(2));
});
$('.total').text(sum);
}
Try this,
function update_amounts(){
var sum = 0.0;
$('#basketorder > tbody > .product').each(function() {
var optionsamount = 0.0;
$(this).find('.selectedoptionselect option:selected').each(function(){
optprice = $(this).attr('price');
optionsamount+= parseFloat(optprice);
})
var qty = $(this).find('.qty option:selected').val();
var price = $(this).find('.price').val();
var amount = (qty*price);
sum+= (amount + optionsamount);
$(this).find('.amount').text(''+ amount.toFixed(2));
});
$('.total').text(sum);
try this
function update_amounts(){
var sum = 0.0;
$('#basketorder > tbody > .product').each(function() {
var optionsamount = 0.0;
$('.selectedoptionselect option:selected').each(function(){
optprice = $(this).attr('price');
optionsamount+= parseFloat(optprice);
})
var qty = $(this).find('.qty option:selected').val();
var price = $(this).find('.price').val();
var amount = (qty*price);
sum+= (amount + optionsamount);
$(this).find('.amount').text(''+ amount.toFixed(2));
});
$('.total').text(sum);
}

Categories

Resources