You have $50 in your wallet. You must pay a $3 fine for each late book (b). Then, a friend gives you $10. Write an expression for your remaining money.
Functions can manage state changes like transactions.
function calculateRemaining(lateBooks: number): number {
const startMoney = 50;
const finePerBook = 3;
const gift = 10;
// Expression: 50 - 3b + 10
// Note: Parentheses prioritize the multiplication
return startMoney - (finePerBook * lateBooks) + gift;
}
// 5 late books:
console.log(calculateRemaining(5));
// 50 - (3 * 5) + 10
// 50 - 15 + 10 = 45