dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
76 lines
2.6 KiB
Markdown
76 lines
2.6 KiB
Markdown
---
|
|
id: 6a22d77ddf034bc4e35b1d55
|
|
title: "Challenge 348: Loan Calculator"
|
|
challengeType: 28
|
|
dashedName: challenge-348
|
|
---
|
|
|
|
# --description--
|
|
|
|
Given a loan amount, annual interest rate percentage, and fixed monthly payment, return an array of remaining balances after each monthly payment until the loan is paid off.
|
|
|
|
- Each month, interest is calculated on the remaining balance using the monthly interest rate: `(annual rate / 100) / 12`, then the monthly payment is subtracted.
|
|
- Return each remaining balance rounded to the nearest dollar.
|
|
- Include the loan amount in the returned array. The first element in the array will always be the loan amount, and the last element of the array will always be 0.
|
|
|
|
# --hints--
|
|
|
|
`getLoanSchedule(1000, 0, 200)` should return `[1000, 800, 600, 400, 200, 0]`.
|
|
|
|
```js
|
|
assert.deepEqual(getLoanSchedule(1000, 0, 200), [1000, 800, 600, 400, 200, 0]);
|
|
```
|
|
|
|
`getLoanSchedule(1000, 5, 200)` should return `[1000, 804, 608, 410, 212, 13, 0]`.
|
|
|
|
```js
|
|
assert.deepEqual(getLoanSchedule(1000, 5, 200), [1000, 804, 608, 410, 212, 13, 0]);
|
|
```
|
|
|
|
`getLoanSchedule(10, 50, 1)` should return `[10, 9, 9, 8, 8, 7, 6, 5, 5, 4, 3, 2, 1, 0, 0]`.
|
|
|
|
```js
|
|
assert.deepEqual(getLoanSchedule(10, 50, 1), [10, 9, 9, 8, 8, 7, 6, 5, 5, 4, 3, 2, 1, 0, 0]);
|
|
```
|
|
|
|
`getLoanSchedule(5500, 8, 400)` should return `[5500, 5137, 4771, 4403, 4032, 3659, 3283, 2905, 2525, 2141, 1756, 1367, 977, 583, 187, 0]`.
|
|
|
|
```js
|
|
assert.deepEqual(getLoanSchedule(5500, 8, 400), [5500, 5137, 4771, 4403, 4032, 3659, 3283, 2905, 2525, 2141, 1756, 1367, 977, 583, 187, 0]);
|
|
```
|
|
|
|
`getLoanSchedule(50000, 5.2, 1650)` should return `[50000, 48567, 47127, 45681, 44229, 42771, 41306, 39835, 38358, 36874, 35384, 33887, 32384, 30874, 29358, 27835, 26306, 24770, 23227, 21678, 20122, 18559, 16990, 15413, 13830, 12240, 10643, 9039, 7428, 5810, 4186, 2554, 915, 0]`.
|
|
|
|
```js
|
|
assert.deepEqual(getLoanSchedule(50000, 5.2, 1650), [50000, 48567, 47127, 45681, 44229, 42771, 41306, 39835, 38358, 36874, 35384, 33887, 32384, 30874, 29358, 27835, 26306, 24770, 23227, 21678, 20122, 18559, 16990, 15413, 13830, 12240, 10643, 9039, 7428, 5810, 4186, 2554, 915, 0]);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
function getLoanSchedule(loanAmount, annualRate, monthlyPayment) {
|
|
|
|
return loanAmount;
|
|
}
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
function getLoanSchedule(loanAmount, annualRate, monthlyPayment) {
|
|
const monthlyRate = (annualRate / 100) / 12;
|
|
const balances = [loanAmount];
|
|
let balance = loanAmount;
|
|
|
|
while (balance > 0) {
|
|
balance += balance * monthlyRate;
|
|
balance -= monthlyPayment;
|
|
balances.push(Math.round(Math.max(balance, 0)));
|
|
}
|
|
|
|
return balances;
|
|
}
|
|
```
|