Closed Thread Icon

Topic awaiting preservation: stumped (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8488" title="Pages that link to Topic awaiting preservation: stumped (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: stumped <span class="small">(Page 1 of 1)</span>\

 
jive
Paranoid (IV) Inmate

From: Greenville, SC, USA
Insane since: Jan 2002

posted posted 01-24-2003 18:01

So in the javascript book I'm studying it says to do the following:

quote:
Create a for loop that will output the numbers through 200. Create code that ensures there is a space between each number. Create code that will cause a line break after the output reaches 10, 20, 30, 40 and so on. Create code that outputs the value of the loop counter variable after the for loop is exited.



What I have so far:
<SCRIPT LANGUAGE="JavaScript">

for(var i = 0; i < 200; i++) {

document.write(i + " ");

}

//-->
</SCRIPT>

my problem is I have no Idea how to add a line break after the loop reaches increments of 10. I've tried
<SCRIPT LANGUAGE="JavaScript">

for(var i = 0; i < 200; i++) {

if (i % 10) continue;

document.write(i + " ");
}

//-->
</SCRIPT>

but that obviously only writes the increments of 10. I thought a if statement like

if (i % 10) {

"<P>"

}
But theres no method or property..... so I'm stumped. Can school me a bit on this?





[This message has been edited by jive (edited 01-24-2003).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 01-24-2003 19:33

You're on the right track.

First of all, you want to output the line break *after* you output every 10th number. So I would recommend this structure:

for (...) {
document.write(i + ' ');
if (this is a multiple of 10) document.write('<br>');
}

Notice that a line break can be ouputted by writing a <br> tag to the document.

The only other problem is that you're not properly calculating whether a i is a multiple of 10 or not. This:

i % 10

will give you the result of subtracting the nearest multiple of 10 that's less than i from i. So if i is, say, 31, it will give you 1. If i is 32, it will give you 2. 33 will give you 3. Etc. The key is that when i is a multiple of 10, i % 10 will return zero! So, to test if i is a multiple of 10, you must say

if (i % 10 == 0)

jive
Paranoid (IV) Inmate

From: Greenville, SC, USA
Insane since: Jan 2002

posted posted 01-26-2003 03:02

oooooooooooohhhhhh. I do get it. *lightbulb goes on* That's cool. I love it when I get a concept. Thanks Slime!!



« BackwardsOnwards »

Show Forum Drop Down Menu