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
https://forums.phpfreaks.com/topic/168993-solved-repeating-numbers-even-or-odd/
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...

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>

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.