Kata with two numeric parameters

I was posed a Kata problem where I had to write a function which took two numeric parameters and if the sum of the parameters was even the return would be false, whereas odd would be true.

My solution was as follows

function res(num1, num2){

var num3 = num1 + num2;

if (num3 % 2 === 0){

return false;

}

else

{return true; }

}

This was I believed at the time a simple way of solving a not very complex problem.  Bottom line if the sum of the two numbers was divisible by 2 hence even then then return true, if not divisible by 2.  This was a way to solve the problem, the great thing about solving problems is that there maybe many ways to solve the same problem.  One of the clever  ways of solving the same problem was much more efficient and did not require an if else statement, nor another variable to assign the result of the two parameters.  Have a look:

function res(num1, num2){

              return num1 % 2 !== num2 % 2; }

Effectively this  solution has done away with so much of the redundant code that I wrote. Clever.

Leave a Reply

Your email address will not be published. Required fields are marked *