And as an extra, here is a version in which the checks are also sorted:
const generateReport = checkbook => { let current = 0; return checkbook.replace(/[^0-9a-z\. \n]/gi, "") .split("\n") .sort((a,b) => { const arrA = a.split(" "); const arrB = b.split(" "); if (arrA.length > arrB.length) { return 1; } else if (arrB.length > arrA.length) { return -1; } else { return parseInt(arrA[0]) > parseInt(arrB[0]) ? 1 : -1; } }) .map((val, index) => { current = index === 0 ? val : (current - val.split(" ")[2]).toFixed(2); return index === 0 ? "Original Balance: " + val : val + ` ${current}`; }) .join("\n") .concat(`\nTotal expense: ${(checkbook.split("\n")[0] - current).toFixed(2)}`) .concat(`\nAverage expense: ${((checkbook.split("\n")[0] - current)/(checkbook.split("\n").length-1) || 0).toFixed(2)}`); }
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.
And as an extra, here is a version in which the checks are also sorted: