Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
How to convert recursion to loop?

I need convert this code to loops , but i have no idea how .

let object = {
  x: 250,
  a: 5,
  b: 5,
  c: {
    ac: 5,
    acd: 10
  }
};

let objectPropertiesSum = object => {
  let sum = 0;
  for (const value of Object.values(object)) {
    if (typeof(value) === "number") {
      sum += value;
    } else if (typeof(value) === "object") {
      sum += objectPropertiesSum(value);
    }
  }
  return sum;
};
console.log(objectPropertiesSum(object));

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Where are you getting stuck? Maybe this can help you get started.

function sumValues(obj) {
  const stack = [obj]
  let sum = 0
  let o, v
  while (stack.length) {  /* as long as there are items on the stack */
    o = /* get an item off the stack */
    for (v of /* obj values */) {
      if (/* v is a number */)
        /* update the sum */
      else if (/* v is an object */)
        /* update the stack */
      else
        /* v is not a Number or an Object */
    }
  }
  return sum
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!