/    Sign up×
Community /Pin to ProfileBookmark

Trouble with simple script

Thanks in advance for the help.

I am very new to JavaScript. I am interested in learning how to properly write it. I was wanting to start with something relatively simple, but I find myself trying to pull out my hair to make the script work properly. :p

All I was trying to do is write a script that would allow a user to place a number in a text box, and when the user hits submit, a JavaScript function would calculate shipping and handling. Unfortunately, the script keeps wanting to join two variables together instead of performing the operation. Any help would be appreciated.

I am also open for suggestions on how to clean up my script because I know it is sloppy, and probably has a lot of unnecessary lines, but I am new at this.

Here is my code:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<script type=”text/javascript”>
function shippingHandling() {
var purchasePrice = document.calcShipping.purchase_price.value;
var shippingHandling = 1.50;
var grandTotal = purchasePrice + shippingHandling;
var newPrice = purchasePrice + addShipHand;
var addShipHand = purchasePrice * 10/100;
if(purchasePrice <= 25) {
window.alert(“Your total cost is $” + purchasePrice + ” plus $” + shippingHandling + “, which is equal to $” + grandTotal + “”);
return true;
}
if(purchasePrice > 25) {
window.alert(“Your total cost is $” + purchasePrice + ” plus $” + addShipHand + “, which is equal to $” + newPrice + “”);
return true;
}
}

</script>
</head>

<body>
<form name=”calcShipping” action=”” onsubmit=”return shippingHandling()” method=”post”>
<table>
<tr>
<td>Purchase Price:</td><td><input type=”text” name=”purchase_price”></td>
</tr>
<tr>
<td>&nbsp;</td><td><input type=”submit” value=”Submit”></td>
</tr>
</table>
</form>
</body>

</html>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJun 13.2011 — JS works with strings and numbers, but the '+' character treats variables as strings

unless given specific direction.

So
<i>
</i>var grandTotal = purchasePrice + shippingHandling;

should be changed to:
<i>
</i>var grandTotal = Number(purchasePrice) + Number(shippingHandling);

or use parseInt() if integers or parseFloat() if not

or change the strings to numbers by multiplying by 1 as in
<i>
</i>var grandTotal = (purchasePrice*1) + (shippingHandling*1);



BTW: You should use [ code] and [ /code] tags (without the spaces)

to make it easier for forum members to read and reply about your scripts.
Copy linkTweet thisAlerts:
@jrg_newtojsauthorJun 13.2011 — Thanks JMRKER,

I had just read about placing Number() to make the string a number in another thread. And I did get it to work. I ended up placing the Number() in the first variable of the function

<i>
</i>var purchasePrice = Number(document.calcShipping.purchase_price.value);


It worked fine, but then I noticed that I placed my var labeled "newprice" before the var "addShipHand". I was trying to reference a variable that had not even been initialized yet! Thanks for the help. Over time I will get better at this.
Copy linkTweet thisAlerts:
@JMRKERJun 13.2011 — You're most welcome.

Happy to try to help.

Good Luck!

?
×

Success!

Help @jrg_newtojs spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...