PDA

View Full Version : Why won't my JavaScript display?


comrade
May 15, 2003, 04:26 AM
Hey all,

I am having trouble with some easy JavaScript. I'm trying to get ahead on a lab I have tomorrow. If you are familar with JavaScript, the assignment probably seems really easy: (http://www.cs.ucf.edu/courses/cop2500/assignments/cop2500lab02.html)

I am not really concerned with if the code is doing what it is supposed to. That is something for me to learn (plus my code isn't really finished). What I want to know is why I can't see it in a web browser. I can see my html, but nothing from the JavaScript. Any insight would be appreciated. The code will now follow.

P.S.

I was using Camino to preview the html file off my hard drive, if that matters. Thanks to all in advance!

<html>

<head>

<title>
COP2500 Assignment 2
</title>
</head>

<body>

<b><center>Far2Cent()</center></b>
<p>

<script language="JavaScript">

var far1 = 32;
var far2 = 78;
var far3 = 212;
var cent1 = + 5 * (far1 - 32) / 9;
var cent2 = + 5 * (far2 - 32) / 9;
var cent3 = + 5 * (far3 - 32) / 9;
document.write(+ far1 "degrees F = " cent1 "degress C <br>");
document.write(+ far2 "degrees F = " cent2 "degress C <br>");
document.write(+ far3 "degrees F = " cent3 "degress C <br>");

</script>


<p>

<b><center>PriceCalc()</center></b>
<p>

<script language="JavaScript">

var subtotal = 57.24;
var taxrate = 0.06;
var salestax = subtotal * taxrate;
var total = subtotal + salestax;
document.write("Salestax at" taxrate "for a subtotal of" subtotal "is:" salestax);
var salestax = Math.round(salestax * 100);
document.write(salestax "<br>");
var salestax = salestax/100;
document.write(salestax "<br>");
document.write(total);
</script>

</body>

</html>

robbieduncan
May 15, 2003, 05:21 AM
I have never seem document.write(+ ... before. + is a binary operrator so this is porbably causing the JavaScript interpreter to stop. What did you think/want these lines to do?

barkmonster
May 15, 2003, 06:00 AM
There's a few typos in the code that are causing the errors, I've broken it down for you.

The script tags

It's normal practice to put comments in script tags.

<script language="Javascript"><!--

SCRIPT CODE GOES HERE

//-->
</script>

The first script

var far1 = 32;
var far2 = 78;
var far3 = 212;
var cent1 = + 5 * (far1 - 32) / 9;
var cent2 = + 5 * (far2 - 32) / 9;
var cent3 = + 5 * (far3 - 32) / 9;

would be a lot neater like this (note, there's no need for the + at the beginning of the cent variables) :

var far1 = 32; far2 = 78; far3 = 212;
cent1 = (5 * (far1 - 32) / 9);
cent2 = (5 * (far2 - 32) / 9);
cent3 = (5 * (far3 - 32) / 9);

document.write(+ far1 "degrees F = " cent1 "degress C <br>"); is wrong.

You need to put +'s between variables when using document.write, also, it's a good idea to add spaces at the start and end of the quotes. If you change the document.write lines to this :

document.write(far1 + " degrees F = " + cent1 + " degrees C <br>"
+ far2 + " degrees F = " + cent2 + " degrees C <br>"
+ far3 + " degrees F = " + cent3 + " degrees C <br>");

it will compile with no errors and display like this :

32 degrees F = 0 degrees C
78 degrees F = 25.555555555555557 degrees C
212 degrees F = 100 degrees C


Without the spaces inbetween the quotation marks it wouldn't display correctly and look like this :

32degreesF =0degrees C
78degrees F =25.555555555555557degrees C
212degrees F =100degrees C


You could compress the whole first part of the script into 4 simple lines by using arrays, this is a more efficient (and lazy) way of doing the same thing :

<script language="JavaScript"><!--

var far = new Array(32,78,212); cent = new Array();

for(var n=0;n<3;n++){
cent[n] = (5 * (far[n] - 32) / 9);
document.write(far[n] + " degrees F = " + cent[n] + " degrees C <br>");
}

//-->
</script>


The second script

like the first script, I've reduced the number of var statements to 1, also, there's no need to decare the salestax variable more than one.


var subtotal = 57.24;
var taxrate = 0.06;
var salestax = subtotal * taxrate;
var total = subtotal + salestax;

would become

var subtotal = 57.24; taxrate = 0.06;
salestax = subtotal * taxrate;
total = subtotal + salestax;

The document write has the same typo as before, you need to add spaces in the quotes to make it readable and use + to add the different parts together. Also, there's no need to use 2 document.write's when 1 will do for the last few lines and I've added a document.close() to the end of the script because it's good practice to do this.

document.write("Salestax at" taxrate "for a subtotal of" subtotal "is:" salestax);
var salestax = Math.round(salestax * 100);
document.write(salestax "<br>");
var salestax = salestax/100;
document.write(salestax "<br>");
document.write(total);

would become

document.write("Salestax at " + taxrate + " for a subtotal of " + subtotal + " is: " + salestax);
salestax = Math.round(salestax * 100);
document.write(salestax + "<br>");
salestax = salestax/100;
document.write(salestax + "<br>" + total);
document.close();

The whole script would look like this :

<html>
<head>
<title>COP2500 Assignment 2</title>
</head>
<body>

<b><center>Far2Cent()</center></b><p>

<script language="JavaScript"><!--

var far = new Array(32,78,212); cent = new Array();

for(var i=0;i<3;i++){
cent[i] = (5 * (far[i] - 32) / 9);
document.write(far[i] + " degrees F = " + cent[i] + " degrees C <br>");
}

//-->
</script>
</p>

<b><center>PriceCalc()</center></b><p>

<script language="JavaScript"><!--

var subtotal = 57.24; taxrate = 0.06;
salestax = subtotal * taxrate;
total = subtotal + salestax;

document.write("Salestax at " + taxrate + " for a subtotal of " + subtotal + " is: " + salestax);
salestax = Math.round(salestax * 100);
document.write(salestax + "<br>");
salestax = salestax/100;
document.write(salestax + "<br>" + total);
document.close();

//-->
</script>
</p>
</body>
</html>

You might want to round down some of the results so they have less decimal places, unless they're supposed to have such an accurate display.

hope this helps.

comrade
May 15, 2003, 02:34 PM
Thanks for the tips guys. The +'s were in my script because I misinterperted what I neeed them for, but now I see they are used in a different manner. I don't think it mattered how accurate the numbers were, just that they were accurate.