Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

add even numbers recursion js

const recursiveEvenSum = (num, sum = 0) => {
   num = num % 2 === 0 ? num : num - 1;
   if(num){
      return recursiveEvenSum(num - 2, sum+num);
   }
   return sum;
};
console.log(recursiveEvenSum(12));
console.log(recursiveEvenSum(122));
console.log(recursiveEvenSum(23));
console.log(recursiveEvenSum(10));
console.log(recursiveEvenSum(19));
 
PREVIOUS NEXT
Tagged: #add #numbers #recursion #js
ADD COMMENT
Topic
Name
4+6 =