• 0

[JavaScript] Read in two integers, display if SUM is even or odd.


Question

6 answers to this question

Recommended Posts

  • 0

Should be easy enough, you need to use the modulo operator to figure out the remainder:

var isRemainderEven = function(first, second)
{
	var result = first + second;
	return ((result % 2) == 0);
};

Link to comment
Share on other sites

  • 0

Edit: Guess I should have refreshed before posting. :p

This is what you're going to do:

1. Read in the two numbers and store them in separate variables.

2. Add the two numbers and store the result.

3. Perform a modulo operation with 2 as the divisor to see if there is a remainder. (someVariable % 2)

4. If the result of the above operation is zero, then you know that the sum is even.

Link to comment
Share on other sites

  • 0

The only thing I would suggest over Antaris is prefixing your values with + signs. That will ensure they are in fact evaluated as integers, as opposed to strings. Since they are passed to the function from a user input, Javascript may very well default them to strings and a set of values like 2 + 2 would return 22 instead of 4.

is_even = function(first, second) {
	 return ((+first + +second) % 2 == 0) ? 'EVEN' : 'ODD';
 };

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.