Closure Exercise
foreclosure.js
file in your editor.$ npm install
to install all needed packages for this project.$ npm test
run the tests and have them continually watch for changes and re-run tests automatically.Use strict mode for this exercise.
A global variable named data
is available to you. This variable is an object.
Declare a function named loan()
that takes 0
arguments
Write the following statements inside the loan()
function block
account
using const, with an initial value being a literal object having the following properties and values:borrowed
, value : 550000
balance
, value : 286000
monthlyPayment
, value : 1700
defaulted
, value : 0
defaultsToForeclose
, value : 5
foreclosed
, value : false
missPayment
that takes 0
arguments.defaulted
property of the account
variable and increase it’s value by 1
.account.defaulted
is greater than or equal to account.defaultsToForeclose
, will run the following statement:foreclosed
property of the account
object to true
getBalance
, value : an unnamed function expression that takes 0
arguments. balance
by accessing that property of the locally scoped variable account
. receivePayment
, value : an unnamed function expression that takes 1
argument named amount
. amount
is less than account.monthlyPayment
:missPayment
function with 0
argumentsbalance
property of the account
variable, by the value of the amount
parameter. getMonthlyPayment
, value : an unnamed function expression that takes 0
arguments. monthlyPayment
by accessing that property of the locally scoped variable account
. isForeclosed
, value : an unnamed function expression that takes 0
arguments. foreclosed
by accessing that property of the locally scoped variable account
. Declare a function named borrower()
that takes 1
argument named loan
Write the following statements inside the borrower()
function block
account
using const, with an initial value being a literal object having the following properties and values:monthlyIncome
, value : 1350
funds
, value : 2800
loan
, value : loan
getFunds
, value : an unnamed function expression that takes 0
arguments. funds
by accessing that property of the locally scoped variable account
. makePayment
, value : an unnamed function expression that takes 0
arguments. account.funds
is greater than the value of the loan’s monthly paymentaccount.funds
by the value of the loan’s monthly paymentreceivePayment
function, and pass in the value of the loan’s monthly payment receivePayment
function, and pass in the value of the account
‘s current funds
funds
property of account
to 0
payDay
, value : an unnamed function expression that takes 0
arguments. funds
property of account
by the value of the property monthlyIncome
of account
loan
function and assign it’s return value to the variable data.stevesLoan
. borrower
function passing in the argument data.stevesLoan
and assign it’s return value to the variable data.steve
. while
loop that runs the following statement while data.stevesLoan
is not foreclosed:data.steve
invokes payDay
data.steve
invokes makePayment
data.monthsUntilEvicted
by 1
All tests should be passing.
The value of monthsUntilEvicted
should be 13.
You can tweak some of hardcoded private values such as loan account.monthlyPayment
and borrower account.monthlyIncome
a little to inspect the different possible outcomes. however, be careful! if the house never goes into foreclosure, you will encounter an endless loop.
Adjust the while
loop to also exit when the house has been paid off.
Copyright (c) 2015 Dev League. Licensed under the MIT license.