DEV Community

Discussion on: Daily Challenge #4 - Checkbook Balancing

Collapse
 
kerrishotts profile image
Kerri Shotts

Here's my take (JavaScript). A few notes:

  • I sort by line #, and then by category
  • The first line might actually be blank, so I trim the input
  • Categories are assumed to be single words (no spaces allowed)
  • Output includes digit grouping by current locale
  • Full code (incl some basic tests): gist.github.com/kerrishotts/461a90...

const sanitize = str => str.replace(/[^0-9A-Za-z\.\s]/g, "");

const notBlank = str => str !== "";

const extract = str => {
    const [ line, category, expense ] = str.split(/\s+/);
    return { line: Number(line), category, expense: Number(expense) };
};

const byLineAndCategory = (a, b) => a.line < b.line 
    ? -1 : a.line > b.line 
        ? 1 : a.category < b.category 
            ? -1 : a.category > b.category 
                ? 1 : 0;

const balanceReducer = (
    {openingBalance, totalExpenses, entries}, 
    {line, category, expense}
) => {
    const newTotal = totalExpenses + expense;
    const newBalance = openingBalance - newTotal;
    return {
        openingBalance,
        totalExpenses: newTotal,
        averageExpense: newTotal / (entries.length + 1),
        entries: [ ...entries, {line, category, expense, balance: newBalance }]
    }
};

const round2 = n => (Math.round(n * 100) / 100)
    .toLocaleString(undefined, {
        style: "decimal",
        minimumFractionDigits: 2,
        useGrouping: true
    });

const balanceCheckbook = (checkbook) => {
    const [openingBalanceStr, ...entries] = 
        sanitize(checkbook)
        .trim()
        .split("\n")
        .filter(notBlank);

    const openingBalance = Number(openingBalanceStr);

    const initialState = { 
            openingBalance, 
            entries: [], 
            averageExpense: 0, 
            totalExpenses: 0
    };

    const report = 
        entries
        .map(extract)
        .sort(byLineAndCategory)
        .reduce( balanceReducer, initialState );

    return `
Original Balance: ${round2(report.openingBalance)}
${report.entries.map(({line, category, expense, balance}) =>
`${line} ${category} ${round2(expense)} Balance ${round2(balance)}`
).join("\n")}
Total Expenses: ${round2(report.totalExpenses)}
Average Expense: ${round2(report.averageExpense)}
`.trim();
};

Enter fullscreen mode Exit fullscreen mode
Collapse
 
zerquix18 profile image
I'm Luis! \^-^/

I think is the most scalable solution since you first move all the data to a manipulable format, deal with it and then output it.