Jump to content

[SOLVED] Repeating numbers - even or odd


twilitegxa

Recommended Posts

I have the following script that lists numbers 1-20 and tells whether the number is even or odd. How can I write it so that it tells that it is even or odd after the number is listed instead of before? Like:

 

1 - Odd

2 - Even

etc...

 

Here is the code I have:

 

<!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 Document</title>
</head>

<body>
<script>
var count = 1
while (count <= 20) {
if (count%2)
   {
   document.write("- Odd");
   }
else
   {
   document.write("- Even");
   }
document.write(count + "<br />");
++count;
}


</script>
</body>
</html>

Link to comment
Share on other sites

This is what is outputting now:

 

- Odd1

- Even2

- Odd3

- Even4

- Odd5

- Even6

- Odd7

- Even8

- Odd9

- Even10

- Odd11

- Even12

- Odd13

- Even14

- Odd15

- Even16

- Odd17

- Even18

- Odd19

- Even20

 

I want it to say:

 

1 - Odd

2 - Even

3 - Odd

4 - Even

etc...

Link to comment
Share on other sites

I got it figured out, thanks!

{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 Document</title>
</head>

<body>
<script>
var count = 1
while (count <= 20) {
if (count%2)
   {
   document.write(count + " - Odd<br />");
   ++count;
   }
else
   {
   document.write(count + " - Even<br />");
   ++count;
   }
   }


</script>
</body>
</html>

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.