'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);


Solution 1:[1]

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; }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1