twilitegxa Posted August 5, 2009 Share Posted August 5, 2009 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> Quote Link to comment Share on other sites More sharing options...
Mardoxx Posted August 5, 2009 Share Posted August 5, 2009 wat Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted August 5, 2009 Author Share Posted August 5, 2009 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... Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted August 5, 2009 Author Share Posted August 5, 2009 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> Quote Link to comment Share on other sites More sharing options...
Mardoxx Posted August 5, 2009 Share Posted August 5, 2009 *cough* blonde Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 5, 2009 Share Posted August 5, 2009 Or just this for (count=1; count<=20; count++) { document.write(count+' - '+((count%2)?'Odd':'Even')+'<br />'); } Quote Link to comment Share on other sites More sharing options...
Mardoxx Posted August 6, 2009 Share Posted August 6, 2009 reminds me of that simpsons episode where marge joins the police force.. "women ehhh...they never see the door" Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.