Final answer:
To calculate yearly cashback, a function called cashBack uses different cashback percentages for specified spending ranges: 1% for the first $500, 1.5% for $500 to $1000, 2% for $1000 to $2000, and 3% for amounts above $2000. The total cashback is the sum of cashback calculated within each spending range.
Step-by-step explanation:
Implementing the Cashback Function
To implement a function cashBack that calculates yearly cashback based on the amount spent, we need to consider the different cashback percentages for specific spending ranges. Here is a step-by-step explanation to create such a function:
- Calculate 1% cashback for the first $500 spent.
- For the amount between $500 and $1000, calculate 1.5% cashback.
- For the amount between $1000 and $2000, calculate 2% cashback.
- For any amount above $2000, calculate 3% cashback.
For example, if a student spent $2500 in a year, the cashback would be calculated as:
- $500 * 1% = $5
- Next $500 * 1.5% = $7.50
- Next $1000 * 2% = $20
- Amount above $2000, which is $500, at 3% = $15
Adding these amounts together gives us the total cashback: $5 + $7.50 + $20 + $15 = $47.5