...

View Full Version : Quick javascript help please



Kinda confused
12-16-2010, 04:33 PM
I am new to java and tried writing the code for practice but it doesn't seem to work

<html>
<Head>
<title>Discount</title>
<script language ="Javascript">
<!--
function CalculateBalance(numItems,totPrice,discount)
{ var balance;
balance = (1 - discount)*totPRice;
alert ('Your balance is $'+balance +' Thank You.');
document.write("<br>YOu bought "+ numItems+" items for $"+totPrice);
document.write("<br>Your balance is $"+ balance+" your discount was"+discount*100+"% Thank you");
}
//-->
</script>
</head>

<script language ="Javascript">
<!--
var discount;
var numItems = parseInt(prompt('HOw many time did you buy [ >=12 is 20% and >= 6 is 10% discount]',"0"));
alert ('You bought '+numItems +' items.');
document.write("You bought "+ numItems+" items.");
var totPrice = parseFloat(prompt('The total price that you paid was',"0.0"));
if(numItems >= 12) {discount = .2;}
else if (numItems >=6) {discount+ .1}
else {discount = 0;}
CalculateBalance(numItems,totPrice,discount);

//-->
</script>
</html>

It only prints to the screen the number of items I bought

Philip M
12-16-2010, 06:03 PM
I am new to java ..
Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

else if (numItems >=6) {discount+ .1}

document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded. The first document.write wipes out your original page, including the script.

<script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.
The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.


Login: yes
Password: i dont have one
Password is incorrect

Login: yes
Password: incorrect

DJCMBear
12-16-2010, 06:51 PM
Your code has a lot of errors not just in the javascript but in the mark-up coding as well, this code does what you want, but instead of just copy and paste it and then use it, I would take time to look over your code and then my code to see where the errors are and that way you will learn as you go.



<!DOCTYPE html>
<html>
<head>
<title>Discount</title>
<script type="text/javascript">
(function($){
/**
* @Function: CalculateBalance :: PUBLIC
* This calculates the discount price
*/
$.CalculateBalance = function(numItems,totPrice,discount) {
var balance;
balance = (1 - discount)*totPrice;
alert('Your balance is $'+balance+' Thank You.');
return "You bought "+numItems+" items for $"+totPrice+
"\nYour balance is $"+balance+" your discount was "+
discount*100+"% Thank you";
};

/**
* @Function: Discount :: PUBLIC
* This returns the final discount price
*/
$.Discount = function() {
var discount,
callBack,
totPrice,
numItems = parseInt(prompt('How many times did you buy\n[ >=12 is 20% and >= 6 is 10% discount]',"0"));
if(numItems) {
alert('You bought '+numItems+' items.');
totPrice = parseFloat(prompt('The total price that you paid was',"0.0"));
if(numItems >= 12) {discount = .2;}
else if (numItems >= 6) {discount = .1;}
else {discount = 0;}
alert($.CalculateBalance(numItems,totPrice,discount));
}
};
})(window);
</script>
</head>
<body>
<input type="button" value="Run Discount Checker" onclick="Discount();" />
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum