...

View Full Version : Need help with some code



anonymous9
01-22-2012, 05:11 PM
For some reason when I enter the 5 numbers into the prompts within less than 10 seconds I am told it took 19 hours to do this. It should say 0 hours since it was done in a matter of seconds. How do I fix this?


<?xml version = "1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang="en" >

<body>
<h2>Refresh the page to see the new time.</h2>
<script type="text/javascript">
var int1;
var int2;
var int3;
var int4;
var int5;



<!--
var now=new Date();
document.writeln(now);
document.writeln("<br /><br />");
var y, mo, dy, dt, h, mi, s, num1, num2, num3, num4, num5, total, first, second, third, fourth, fifth;
y=now.getFullYear();
mo=now.getMonth();
dy=now.getDay();
dt=now.getDate();
h=now.getHours();
mi=now.getMinutes();
s=now.getSeconds();

document.writeln("The day of the week is "+dy+"<br />");
document.writeln("The day of the month is "+dt+"<br />");
document.writeln("The month of the year is "+mo+"<br />");
document.writeln("The year is "+y+"<br />");
document.writeln("The hour of the day is "+h+"<br />");
document.writeln("The minute of the hour is "+mi+"<br />");
document.writeln("The second of the minute is "+s+"<br />");
//Prompting the user 5 times for numbers
var start = new Date();
var start1 = new Date();
first = window.prompt("Please Enter The First Number");
//Gets seconds for first number
var finish1 = new Date();
var difference1 = new Date();
difference1.setTime(finish1.getTime() - start1.getTime());


var start2 = new Date();
second = window.prompt("Please Enter The Second Number");
//Gets seconds for second number
var finish2 = new Date();
var difference2 = new Date();
difference2.setTime(finish2.getTime() - start2.getTime());


var start3 = new Date();
third = window.prompt("Please Enter The Third Number");
//Gets seconds for third number
var finish3 = new Date();
var difference3 = new Date();
difference3.setTime(finish3.getTime() - start3.getTime());


var start4 = new Date();
fourth = window.prompt("Please Enter The Fourth Number");
//Gets seconds for fourth number
var finish4 = new Date();
var difference4 = new Date();
difference4.setTime(finish4.getTime() - start4.getTime());


var start5 = new Date();
fifth = window.prompt("Please Enter The Fifth Number");
//Gets seconds for fifthnumber
var finish5 = new Date();
var difference5 = new Date();
difference5.setTime(finish5.getTime() - start5.getTime());


num1 = parseInt (first);
num2 = parseInt (second);
num3 = parseInt (third);
num4 = parseInt (fourth);
num5 = parseInt (fifth);

var finish = new Date();
var difference = new Date();
difference.setTime(finish.getTime() - start.getTime());


total = num1 + num2 + num3 + num4 + num5;
product = num1 * num2 * num3 * num4 * num5;
average = (num1 + num2 + num3 + num4 + num5) / 5;

document.writeln("The Sum of the Five Numbers is: "+ total+"<br />" );
document.writeln("The Product of the Five Numbers is: " + product+"<br />" )
document.writeln("The Average of the Five Numbers is: "+ average +"<br />");

document.writeln("Your First Number: "+ num1 + "<br />");
document.writeln("It took " + difference1.getHours() + " Hours " + difference1.getMinutes() + " Minutes and " + difference1.getSeconds() + " Seconds to click <br />");
document.writeln("Your Second Number: "+ num2 +"<br />");
document.writeln("It took " + difference2.getHours() + " Hours " + difference2.getMinutes() + " Minutes and " + difference2.getSeconds() + " Seconds to click <br />");
document.writeln("Your Third Number: "+ num3 +"<br />");
document.writeln("It took " + difference3.getHours() + " Hours " + difference3.getMinutes() + " Minutes and " + difference3.getSeconds() + " Seconds to click <br />");
document.writeln("Your Fourth Number: "+ num4 +"<br />");
document.writeln("It took " + difference4.getHours() + " Hours " + difference4.getMinutes() + " Minutes and " + difference4.getSeconds() + " Seconds to click <br />");
document.writeln("Your Fifth Number: "+ num5 +"<br />");
document.writeln("It took " + difference5.getHours() + " Hours " + difference5.getMinutes() + " Minutes and " + difference5.getSeconds() + " Seconds to click <br />");
document.writeln("The Number of Seconds It Took To Enter 5 Numbers Was " + difference.getSeconds());

// -->
</script>
</body>

</html>

Philip M
01-22-2012, 05:20 PM
document.write or document.writeln() 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.

In fact document.write() is completely obsolete unless you are coding for Netscape 4.

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.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

anonymous9
01-22-2012, 06:39 PM
If document.writeln is obsolete then how else am I supposed to put the output of the result on the screen after the page finishes loading?

Philip M
01-22-2012, 07:55 PM
If document.writeln is obsolete then how else am I supposed to put the output of the result on the screen after the page finishes loading?

DOM methods - write to a <div> with innerHTML

I have the idea that this is another example of students being taught obsolete (20th century) Javascript code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum