Jump to content

Javascript loops and conditions


irken

Recommended Posts

Hi.

I'm trying to split the numbers being output into a new line every 7th number. So far no success. I'm using, plain and simple:

[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title>
<script language="javascript" type="text/javascript">

var i = 1;
while (i <= 49)
{
if (i % 7 == 0)
{
document.writeln('<br />');
}

document.writeln(i);
i++;
}

</script>
</head>

<body>

</body>

</html>

[/code]

Now, this outputs:

[quote]
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 32 33 34
35 36 37 38 39 40 41
42 43 44 45 46 47 48
49
[/quote]

I'm trying to do ([b]added 0's before numbers for better reading/alignment, this does not have to be in the code[/b]):

[quote]
01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
[/quote]

Ideas? Thanks.
Link to comment
https://forums.phpfreaks.com/topic/34262-javascript-loops-and-conditions/
Share on other sites

[quote author=emehrkay link=topic=122483.msg505217#msg505217 date=1168877797]
i%8
[/quote]

Well I tried that. It outputs:

[quote]
1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31
32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47
48 49
[/quote]
I guess I could do

[code]
var i = 1;
while (i <= 49)
{
if (i == 8 || i == 15 || i == 22 || i == 29 || i == 36 || i == 43)
{
document.writeln('<br />');
}

document.writeln(i);
i++;
}

[/code]

But that feels kinda stupid when you can do it using the first version, a much shorter and cleaner way. I just can't figure out why it's not doing as I like.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.