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

204
Views
How can I build an exception handler?

I need to handle "null","yes"/"no" as exeptions that are = to 0 and strings with "4" as points. When they are ordered it should be printed out as it was given as input. I tried to use a for loop to reach the scores but it doesn't work. How can I build an exception handler?

function reorder(people) {
  const arr = people;
  for (let i in arr) {
    for (let a in i.scores  ) {
      if (a === null || a === "yes" || a === "no") {
        a = 0;
      } else if (typeof a === "string") {
        a = Number(a);
      }
    }
  }
  const sorted = arr
    .map((e) => ({
      ...e,
      sum: e.scores.reduce((total, score) => total + score, 0),
    }))
    .sort((a, b) => b.sum - a.sum || a.name.localeCompare(b.name))
    .map(({ sum, ...e }) => e);

  return sorted;
}

const input = [
  { name: "Joe", scores: [1, 2, "4.1"] },
  { name: "Jane", scores: [1, null, 3] },
  { name: "John", scores: [1, 2, 3] },
];

const output = reorder(input);
console.log(output);

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

0

You coud adjust reduce callback and convert the score to number or take zero for adding.

function reorder(people) {
    return people
        .map(e => ({ ...e, sum: e.scores.reduce((total, score) => total + (+score || 0), 0) }))
        .sort((a, b) => b.sum - a.sum || a.name.localeCompare(b.name))
        .map(({ sum, ...e }) => e);
}

const
    input = [{ name: "Joe", scores: [1, 2, "4.1"] }, { name: "Jane", scores: [1, null, 3] }, { name: "John", scores: [1, 2, 3] }],
    output = reorder(input);

console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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!