2

how to get sum of odd, even using reduce method, i have done as show in below code but returning undefined , @js-beginner

//code below

nums= [1,2,3,4,5,6,7,8,9]

function getOddEvenSum(numbers){     
    let{even,odd} = numbers.reduce((acc, cuu) => cuu%2 === 0?acc.even + cuu:acc.odd+cuu,{even:0, odd:0})

    return {even, odd}
  }

console.log(getOddEvenSum(nums)

//output i am getting below

{even:undefined, odd:undefined}
3
  • You can use forEach and add the variables to respective conditions instead. Commented Aug 27, 2021 at 11:31
  • 1
    You don't actually assign incremented value to acc properties, try to change acc.even + cuu:acc.odd+cuu for acc.even += cuu:acc.odd+=cuu and don't forget to return acc itself: numbers.reduce((acc, cuu) => (cuu%2 === 0?acc.even += cuu:acc.odd+=cuu, acc),{even:0, odd:0}) Commented Aug 27, 2021 at 11:33
  • Furthermore, there's no point in destructuring and combining back the same object, you may simply do const getOddEvenSum = numbers => numbers.reduce(.. Commented Aug 27, 2021 at 11:38

4 Answers 4

4

The value that you return from your reduce callback will be the value of acc upon the next invocation/iteration of your array of numbers. Currently, your acc starts off as an object, but as you're only returning a number from your first iteration, all subsequent iterations will use a number as acc, which don't have .even or .odd properties. You could instead return a new object with updated even/odd properties so that acc remains as an object through all iterations:

const nums = [1,2,3,4,5,6,7,8,9];

function getOddEven(numbers){     
  return numbers.reduce((acc, cuu) => cuu % 2 === 0
    ? {odd: acc.odd, even: acc.even + cuu}
    : {even: acc.even, odd: acc.odd+cuu},
  {even:0, odd:0});
}

console.log(getOddEven(nums));

Sign up to request clarification or add additional context in comments.

1 Comment

unnecessary ... spread. just acc.even += cuu is enough. same as odd
4

You can use Array.prototype.reduce like this:

const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9];

const [odds, evens] = nums.reduce(
  ([odds, evens], cur) =>
    cur % 2 === 0 ? [odds, evens + cur] : [odds + cur, evens],
  [0, 0]
);

console.log(odds);
console.log(evens);

Comments

2

This is not how the syntax of reduce works. One possible implementation:

function getOddEven(nums) {
  return nums.reduce(
    ({odd, even}, num) => num % 2 === 0 ?
      {odd, even: even + num} :
      {odd: odd + num, even},
    {odd: 0, even: 0},
  );
}

I would argue that this is not very clear. Since performance is probably not critical, a clearer alternative would be:

function getOddEven(nums) {
  return {
    odd: nums.filter(num => num % 2 == 1).reduce((acc, num) => acc + num),
    even: nums.filter(num => num % 2 == 0).reduce((acc, num) => acc + num),
  };
}

Comments

1

based on your code, you need to return acc

let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]

    function getOddEven(numbers) {
      let {
        even,
        odd
      } = numbers.reduce((acc, cuu) => {
      
        if(cuu%2 == 0) 
        {
        acc.even += cuu    
        }
        else{
        acc.odd += cuu   
        }
        
        return acc
      
      }, 
      {
        even: 0,
        odd: 0
      })
    
      return {
        even,
        odd
      }
    }
    
    console.log(getOddEven(nums))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.