Networking | Hardware | Software | Multimedia | System | Unix&Linux | MBA

Home>>Hardware>>For loop issue!!!

For loop issue!!!

digic25
03-18-2006, 03:35 PM
For some reason when I open this script with any browser nothing is displayed. It should bring up something like the following:

1
4
27
256
etc.

Any help would be appreciated.

<script type="text/javascript">
var m, n;
n = 10;
m = Math.floor(Math.random() * n + 1); //generates a random number between 1
and 10. Then converts value to a whole a number.
for (i = 1; i <= m; i++)
{
document.write(Math.pow(m,i));
document.write("<br>");
}

</script>

Kor
03-18-2006, 03:48 PM
document.write() is not a dynamical method. Further more, is not a "concatenate" method. It will always write once and overwrite the result of the previous document.write(). U should use document.write only once.

Try
var txt=''
for (var i = 1; i <= m; i++)
{
txt += Math.pow(m,i)+'<br>';
}
document.write(txt)

digic25
03-18-2006, 04:04 PM
Thanks for the quick response...

But I'm still getting the same problem of a blank page.


Tom

Kor
03-18-2006, 04:08 PM
I don't think so:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var m, n;
n = 10;
m = Math.floor(Math.random() * n + 1);
var txt=''
for (var i = 1; i <= m; i++)
{
txt += Math.pow(m,i)+'<br>';
}
document.write(txt)
</script>
</head>
<body>

</body>
</html>


It works on any browser. Check whether your browser is javascript enabled or not

felgall
03-18-2006, 10:10 PM
document.write() is not a dynamical method. Further more, is not a "concatenate" method.

Actually it is a concatenate method. Using multiple document.write statements will work provided that they all run before the page finishes loading. The only problem arises when you try to run document.write statements after the page has loaded - that's when they will create a new page.

The other thing to watch for is that the code written by the document.write will be added after the end of the entire Javascript in some browsers rather than at the spot where the write statement appears. This can cause problems is using document.write to write script tags to write further Javascript that contains document.write statements.

Kor
03-18-2006, 10:18 PM
Actually it is a concatenate method. Using multiple document.write statements will work provided that they all run before the page finishes loading. The only problem arises when you try to run document.write statements after the page has loaded - that's when they will create a new page.

The other thing to watch for is that the code written by the document.write will be added after the end of the entire Javascript in some browsers rather than at the spot where the write statement appears. This can cause problems is using document.write to write script tags to write further Javascript that contains document.write statements.
True, I have almost never used document.write(), I prefere innerHTML or DOM methods. But, anyway, the code I have posted must work. I don't sense why digic25 says it does not.


 

TOP

Hyperthreading hurts s
HP sharpens blade PC l
Apple goes for Quad po
Itanium gets scaled do

For more info

HP sharpens blade PC l
Hyperthreading hurts s
can not call click() i
script does not work o
smart menu 
write to an html file 
FireFox doesn't recogn
Changing e.keyCode (e.
Clearing a range creat
UPDATE: My mouse over 

News Archive

onChange Help 
beforeload? vs onload.
Problem with document.
decreasing my javascri
Problem with \\ and \ 
how to stop scrolling?
Change Label Text Colo
Can I forward a page b
2 javascript functions
Validating a textbox 

Related stories:

How do you add a scrollbar on mouseover.
weird getAttribute() in Moz
Firefox probelms with arrays, mystry to me!
Does IE not allow Array inheritence?
A little help with javascript IF/THEN
help me to resolve this checkbox problem
Is VBScript "invisible"?
does anyone use Venkman?
How can I use "enter" to submit a form in a frame using javascript?

Copyright@2004-2005 www.zzcoke.com All Right Reserved

advanced web statistics