← Unit 1 · all exercises
Saved in this browser only

Spend some money

Start with the balance, take the cost away using -=, and return what is left. The balance may go negative.

int afterSpending(int balance, int cost)

Solution.java
Loading editor…
Results

Examples

  • afterSpending(100, 30) → 70
  • afterSpending(50, 50) → 0

There are also 3 hidden tests you can't see. Solve the problem, not the examples.