Jump to content

Javascript odd numbers

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
kentona

kentona

    Newbie

  • Members
  • PipPip
  • 19 posts
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>


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?

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Change
count = count + 1;
into
count = count + 2;


#3
kentona

kentona

    Newbie

  • Members
  • PipPip
  • 19 posts
Oh dear i feel stupid haha cheers for the help i appreciate it.

#4
kentona

kentona

    Newbie

  • Members
  • PipPip
  • 19 posts
Oh also sorry for the double post but how do i go about removing the dialogue box in javascript so it displays the numbers on the browser and not this,

Posted Image

#5
PsychoCoder

PsychoCoder

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Another options for determining an odd number is to use the modulus operator

while(count <= limit)
{
		if(!(count % 2 == 0))
		{
			stroutput += count + "\r\n";
		}
}

SELECT * FROM Users WHERE Clue > 0;
ERROR: 0 results returned
Posted Image

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts

kentona said:

Oh also sorry for the double post but how do i go about removing the dialogue box in javascript so it displays the numbers on the browser and not this,
The easiest way to do this is by putting an empty div on the page. (not noticable until something is put in). Give this div a value for the ID attribute
<div id="output"></div>

Then access this div from Javascript and put the string into it:
var element = Document.getElementById('output');
>>create your string with odd numbers here<<
element.innerHTML = strOutput