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
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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.