A
A
Antonio092020-05-10 19:51:36
JavaScript
Antonio09, 2020-05-10 19:51:36

How can you find the sum of an object in another way?

https://jsfiddle.net/cahkbfx0/

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Tigran Abrahamyan, 2020-05-10
@Antonio09

function recursion(obj) {
  let sum = 0;
  for (let key in obj) {
    if (typeof obj[key] === 'object') {
      sum += recursion(obj[key]);
    } else {
      sum += obj[key];
    }
  }
  return sum;
}

N
Nadim Zakirov, 2020-05-10
@zkrvndm

Try using Object.values()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question