A little late to the party, but trying to work on a few skills. I thought I'd attempt to work on using closures a bit on this one:
const myCheckbook = checkbook(testData); myCheckbook.balanceCheckbook(); function checkbook(input){ let userData = removeClutter(input); let totalBalance = 0; let totalExpenses = 0; let expenseCounter = 0; let averageExpense = 0; function removeClutter(data){ let cleanData = data.replace(/([^a-zA-Z0-9\s\.])/g, ''); return cleanData.split('\n'); } function addExpense(expense){ totalExpenses += expense; totalBalance -= expense; expenseCounter++; averageExpense = totalExpenses / expenseCounter; }; const balanceCheckbook = function(){ for(let i = 0; i<userData.length; i++){ if(i == 0){ totalBalance = userData[0]; console.log('Original_Balance: ' + totalBalance); }else{ let line = userData[i].split(" "); addExpense(parseFloat(line[2])); console.log(userData[i] + " Balance: " + totalBalance.toFixed(2)); } } console.log("Total Expenses: " + totalExpenses.toFixed(2)); console.log("Average expense: " + averageExpense.toFixed(2)); }; return{ userData: userData, balanceCheckbook: balanceCheckbook } }
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.
A little late to the party, but trying to work on a few skills. I thought I'd attempt to work on using closures a bit on this one: