DEV Community

loading...

Discussion on: Daily Challenge #4 - Checkbook Balancing

Collapse
cvanpoelje profile image
cvanpoelje
tidyCheckbook = checkbook => {
  sum = 0;
  rules = checkbook.split("\n");
  rules[0] = `Original_balance: ${rules[0]}`;
  amountOfRules = rules.length-1;
  return rules
    .map(rule => {
      rule = rule.replace(/[;?!{},]/g, "").split(" ");
      return rule =  { id: rule[0], category: rule[1], checkAmount: parseFloat(rule[2]).toFixed(2)}
    })
    .sort((a, b) => a.id - b.id)
    .map(rule => {
      return (`${rule.id} ${rule.category} ${isNaN(rule.checkAmount) ? '':rule.checkAmount}`)})
    .join("\n")
    .concat(`\n======================\nTotal expense: ${sum}\nAverage expense: ${sum / amountOfRules}`
    )
}