Search⌘ K
AI Features

Solution: Equipment Depreciation Tracker

Explore how to create nested functions and use recursion in Dart to build a practical equipment depreciation tracker. Understand scope encapsulation to keep functions private and learn step-by-step recursive calculation techniques to write maintainable and scalable Dart code.

We'll cover the following...
Dart
double calculateDepreciation(double initialValue, int years) {
double applyDepreciation(double currentValue, int yearsLeft) {
if (yearsLeft <= 0) {
return currentValue;
} else {
return applyDepreciation(currentValue * 0.90, yearsLeft - 1);
}
}
return applyDepreciation(initialValue, years);
}
void main() {
double truckValue = 50000.0;
int yearsUsed = 3;
double currentValue = calculateDepreciation(truckValue, yearsUsed);
print(currentValue);
}

Solution explanation

In the main.dart file:

  • Line 1: We define the outer function to act as the public interface for our calculation, ...