user2913279
user2913279

Reputation: 11

JavaScript Coding for Finding Shipping Total

I am having a very hard time with this code. I have been working on it for days and cannot seem to figure it out. Please help!!

Here are the specific I need for the code: Many companies normally charge a shipping and handling charge for purchases. Create a Web page that allows a user to enter a purchase price into a text box and includes a JavaScript function that calculates shipping and handling. Add functionality to the script that adds a minimum shipping and handling charge of $1.50 for any purchase that is less than or equal to $25.00. For any orders over $25.00, add 10% to the total purchase price for shipping and handling, but do not include the $1.50 minimum shipping and handling charge. The formula for calculating a percentage is price * percent / 100. For example, the formula for calculating 10% of a $50.00 purchase price is 50 * 10 / 100, which results in a shipping and handling charge of $5.00. After you determine the total cost of the order (purchase plus shipping and handling), display it in an alert dialog box.

Here is the code I have:

<!DOCTYPE>

<head>
<title>Calculate Shipping</title>

<script type="text/javascript">

function parseInt() {
var salesPrice = document.salesForm.Price.value;
var minCharge = salesPrice + 1.50;
var shipping = salesPrice * 10/100;
if (salesPrice <= 25)
window.alert('Your sales total including shipping is $' + minCharge);
 else 
window.alert('Your sales total including shipping is $' + salesPrice + shipping);
}
</script>
</head>
<body>
<form name="salesForm">
<div >
<p>Enter Your Purchase Price</p>
<input type="text" name="Price" /><br /><br />
<input type="button" name="Calculate" value="Calculate Shipping"
onclick="parseInt ()" />
</div>
</form>
</body>
</html>

Everything works except for the math in the alert box. It will show an incorrect total...

Upvotes: 0

Views: 1020

Answers (3)

bp4D
bp4D

Reputation: 961

Here is the jsFiddle link

function showAlert() {
var salesPrice = parseFloat(document.getElementById('Price').value);    
var minCharge = salesPrice + 1.50;
var shipping = salesPrice * 10/100;
if (salesPrice <= 25)
window.alert('Your sales total including shipping is $' + minCharge);
    else {
window.alert('Your sales total including shipping is $' + parseFloat(salesPrice + shipping));
    }
}

Also parseInt is javascript method so you will see unexpected behaviors if you create method with same name. Read more here

Upvotes: 0

domokun
domokun

Reputation: 3003

It's showing the wrong value because it is concatenating the strings instead of summing the values

You could either sum it up before alert them, like

var salesAndShipping = salesPrice + shipping
window.alert('Your sales total including shipping is $' + salesPrice + shipping);

Or you could simply parenthesized the expression, like:

window.alert('Your sales total including shipping is $' + (salesPrice + shipping) );

Oh and btw, I think you're overwriting a native function by calling yours ParseInt

Upvotes: 0

David Thomas
David Thomas

Reputation: 253318

Input values are always returned as strings, not numbers; to address that:

var salesPrice = parseFloat(document.salesForm.Price.value),
    minCharge = salesPrice + 1.50,
    shipping = salesPrice * 10/100;

The problem is that + will either add numbers together, or concatenate strings:

var num1 = 1,
    num2 = 3,
    num3 = '2';
console.log(num1 + num2); // 4
console.log(num2 + num3); // '32'

References:

Upvotes: 2

Related Questions