EDF Posted April 18, 2009 Share Posted April 18, 2009 (edited) How would I start off structuring a script to read in two integers? Edited May 5, 2009 by EDF587 Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted April 18, 2009 Veteran Share Posted April 18, 2009 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 More sharing options...
0 soothsayer Posted April 18, 2009 Share Posted April 18, 2009 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 More sharing options...
0 C++ Posted April 18, 2009 Share Posted April 18, 2009 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 More sharing options...
0 EDF Posted April 19, 2009 Author Share Posted April 19, 2009 Thanks for the help guys, but I'm going to have to go step by step, I'll post what my sample code is and go from there. I appreciate that. Link to comment Share on other sites More sharing options...
0 EDF Posted April 20, 2009 Author Share Posted April 20, 2009 How would I do this in a non-function format? Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted April 20, 2009 Veteran Share Posted April 20, 2009 if ((a + b) % 2) == 0) { // Even } else { // Odd } Link to comment Share on other sites More sharing options...
Question
EDF
How would I start off structuring a script to read in two integers?
Edited by EDF587Link to comment
Share on other sites
6 answers to this question
Recommended Posts