Hi i'm starting to learn javascript and i'm making a program that will ask the user for a number and then display all odd numbers which are less then or equal to whatever number the user chose. Here is what i have so far, <!-- odd.html --> <html> <head> <title>odd</title> <style type="text/css"> body { background-color:#22DD44; color:#DD22EE; font-size:30; text-align:center; } </style> </head> <body> <script type="text/javascript"> var limit, count, stroutput; count = 1; stroutput = ""; document.write("<h1>Odd</h1>"); limit = prompt("Enter the limit", ""); if (limit == null) { stroutput = ("You clicked cancel. No count will be shown."); } else if (limit == "") { stroutput = ("Limit must be entered."); } else if (limit != parseInt(limit)) { stroutput = ("Limit must be a valid whole number"); } else { while (count <= limit) { stroutput = stroutput + count + "\n"; count = count + 1; } } alert (stroutput); document.write("<br /><br />"); document.write("<br /><br />"); document.write("<input type='button' name='cmdReload'"); document.write(" value='Do again' onclick='window.location.reload()' />"); </script> </body> </html> Code (markup): As you can see so far it just shows you all numbers but i need it to only show odd numbers. How would i do this? EDIT Nevermind i got it going.
Here is the code that you want <!-- odd.html --> <html> <head> <title>odd</title> <style type="text/css"> body { background-color:#22DD44; color:#DD22EE; font-size:30; text-align:center; } </style> </head> <body> <script type="text/javascript"> var limit, count, stroutput; count = 1; stroutput = ""; document.write("<h1>Odd</h1>"); limit = prompt("Enter the limit", ""); if (limit == null) { stroutput = ("You clicked cancel. No count will be shown."); } else if (limit == "") { stroutput = ("Limit must be entered."); } else if (limit != parseInt(limit)) { stroutput = ("Limit must be a valid whole number"); } else { while (count <= limit) { if(count%2==1){ stroutput = stroutput + count + "\n"; } count = count + 1; } } alert (stroutput); document.write("<br /><br />"); document.write("<br /><br />"); document.write("<input type='button' name='cmdReload'"); document.write(" value='Do again' onclick='window.location.reload()' />"); </script> </body> </html> Code (JavaScript):