answersLogoWhite

0

You can do this with a fairly simple algorithm. First, create a container div in you HTML to hold your results.

Then you'll want to define a function. I'll call mine evenNumbersInRange().

This function will need to take two parameters, representing the two ends of the range.

function evenNumbersInRange( x, y )

{

}

Alright. The first step is to determine which number represents the top of the range, and which represents the bottom. For this, you can use a simple if-than-else statement.*

function evenNumberInRange( x, y )

{

if ( x < y)

{

var bottom = x;var top = y;

} else {

var bottom = y;var top = x;

}

}

Now, we need to make sure that the bottom of the range is an even number. We do this by using the modulus operator (%) which will return the remainder of the first operand divided by the second (i.e. 5 % 2 = 1, 11 % 4 = 3) If it turns out that the number isn't even (that is, that the number % 2 isn't 0) then we'll just add one, because the an odd number plus one is even.

function evenNumberInRange( x, y )

{

if ( x < y)

{

var bottom = x;var top = y;

} else {

var bottom = y;var top = x;

}

if ( bottom % 2 != 0 )

{

bottom++;

}

}

Alright, now all we need to do is add 2 to our bottom number until we hit the top number. There are several ways of looping through this kind of set, but the best one (imho) is a while loop. The while construct is best used when you know you have a very clear condition that will end the loop, but you're not sure how many iterations will happen before then. We'll use a while loop, and check to see if the bottom number is greater than or equal to the top number before we stop. We're using greater-than-or-equal-to because this we'll be incrementing by 2 from an even number, and we might go right past the top number if it's not even.

function evenNumberInRange( x, y )

{

if ( x < y)

{

var bottom = x;var top = y;

} else {

var bottom = y;var top = x;

}

if ( bottom % 2 != 0 )

{

bottom++;

}

while(bottom <= top)

{

var ourEl = document.getElementById('test');ourEl.innerHTML = ourEl.innerHTML + ('

'+bottom+"

");bottom = bottom+2;}

}

The three lines of code inside the while loop get the element into which we're going to write our results (this line could be move outside the loop), get that elements innerHTML and adds a new paragraph to it, and increments the bottom variable by 2.

You can see this all in the jsFiddle I've included in the related links.

* In my jsFiddle, I use the ternary operator rather than if-then-else construct. It works the same, it's just easier to read.

** This solution assumes you want to be inclusive with your range. If you want to exclude the bottom and top numbers, then simply add 2 in the else portion of the check for bottom being even, and change the while condition to less-than instead of less-than-or-equal-to.

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

Write a shell script to direct the odd and even numbersgiven by the user to the respective files name oddtxt and eventxt?

Here is that kind of script: #!bin/bash # Checking parameters number, we need two parameters if [ $# -ne 1 ] then echo "Wrong number of parameters, please try again." exit 1 fi # Checking if number is odd or even if [ $[$1 % 2] -eq 0 ] then echo "Even" # Write number to even.txt file echo $1 &gt;&gt; even.txt else echo "Odd" # Write number to odd.txt file echo $1 &gt;&gt; odd.txt fi exit 0 This bash script accepts one parameter - number and if number is even it is written into end of even.txt file and if odd into odd.txt.


How do you write the word beach in phonetic script?

Well its about a phn conversation i cnt even understand that pls answer our question


How do you write C program to print a Series of Odd Numbers and Even Numbers.?

Generate a random number in half the range you are interested in. If generating odd values, subtract 1 from the upper bound of the range. That is, if the range is 0 to 100, then generate a random number in the range 0 to 50 for even numbers and 0 to 49 for odd numbers. Double the generated number to obtain the even value, or double it and add 1 to obtain the odd value.


How can you use range if the number of numbers is even?

Range is to subtract the largest number from the least number so even if you had an even amount of numbers you would have to do pretty mich the same thing.


How do you write an odd number with even numbers?

If you add, subtract, or multiply two even numbers, you will get an even number. If you divide an even number by another even number, you may get an even number, an odd number, or even a fraction.


How do you create an odd or even script using PHP?

To determine whether a given number is odd or even: function odd_even($i) { return ($i % 2 == 0 ? 'even' : 'odd'); }


Does tulu language has script?

Yes, it does. It is derived from the Grantha script, and very closely resembles the Malayalam script. Tulu does not have a vast body of literature, however, and the script has currently fallen out of use. Palm leaf manuscripts in the Tulu script can be found in plenty even to this day. The earliest recorded literary work in Tulu is a 15th century work called Tulu Mahabharatho.Currently, the Kannada script is used to write Tulu.


Write largest eight digit even number?

99,999,998


Does an orange have an even number of segments?

It depends on the orange. The number of segments usually range from 9-11. So if there are 10, then, yes, it would be an even number.


Who wrote the song break even?

The Script


When did the musical group The Script release the song Break Even?

The song "Break Even" which is also known as "Falling To Pieces" was released on 29 September, 2009. The song is from The Script's debut album called "The Script".


What are the even numbers below 100?

The get a list of all even numbers, write the number 2, then slip the next number (3) and write the number 4. Continue by skipping every other number, which will be the odd numbers. Alternatively, write a consecutive list of all of the numbers from 1 to 50, then multiply each one by 2. The products are all of the multiples of 2, which are even numbers.