My solution :) Also available in my github repo: github.com/MohamedElidrissi/Daily-...
function balanceCheckbook(checkbook) { checkbook = checkbook.replace(/[^A-Za-z0-9\.\n ]/g, ''); const reports = checkbook.match(/[0-9]+\s[A-Za-z]+\s[0-9]+(\.[0-9]{1,2})?/g); const expenses = checkbook.match(/ [0-9]+(\.[0-9]{1,2})?/g); const totalExpenses = expenses .reduce((p, c) => parseFloat(p) + parseFloat(c)) .toFixed(2); let balance = checkbook.match(/^[0-9]+(\.[0-9]{1,2})?/g)[0]; let output = `Original Balance: ${balance}\n`; reports.forEach((report, index) => { balance -= expenses[index]; output += `${report} Balance ${balance.toFixed(2)}\n`; }); output += `Total expense ${totalExpenses}\n`; output += `Average expense ${(totalExpenses / expenses.length).toFixed(2)}`; return output; }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
My solution :)
Also available in my github repo: github.com/MohamedElidrissi/Daily-...