Skip to content

ChazAttack73/foreclosure

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Foreclosure

Closure Exercise

Fork and clone this project. Open the foreclosure.js file in your editor. Run gulp watch to auto-run tests. Follow the instructions below, and every time you see this symbol Test an additional test should pass.

Strict Mode

Use strict mode for this exercise. Test

Initialize variables

  1. Declare a variable named steve. Test
  2. Declare a variable named stevesLoan. Test
  3. Declare a variable named month, with an initial value of 0. Test
  4. Declare a variable named monthsUntilEvicted. Test

loan()

Declare a function named loan() that takes 0 arguments Test

Write the following statements inside the loan() function block

  1. Declare a variable named account, with an initial value being a literal object having the following properties and values:
  • key : borrowed, value : 550000
  • key : balance, value : 286000
  • key : monthlyPayment, value : 1700
  • key : defaulted, value : 0
  • key : defaultsToForeclose, value : 5
  • key : foreclosed, value : false
  1. Declare a function named missPayment that takes 0 arguments.
  • Access the defaulted property of the account variable and increase it's value by 1.
  • Write a conditional that, when the value of account.defaulted is greater than or equal to account.defaultsToForeclose, will run the following statement:
    • set the value of the foreclosed property of the account object to true
  1. returns a literal object with the following properties:
  • key : getBalance, value : an unnamed function expression that takes 0 arguments. Test
    • Create a closure by returning the value of balance by accessing that property of the locally scoped variable account. Test
  • key : receivePayment, value : an unnamed function expression that takes 1 argument named amount. Test
    • Create a conditional statement that will run the following statements when amount is less than account.monthlyPayment :
      • invoke the missPayment function with 0 arguments
    • Finally, decrement the value of the balance property of the account variable, by the value of the amount parameter. Test
  • key : getMonthlyPayment, value : an unnamed function expression that takes 0 arguments. Test
    • Create a closure by returning the value of monthlyPayment by accessing that property of the locally scoped variable account. Test
  • key : isForeclosed, value : an unnamed function expression that takes 0 arguments. Test
    • Create a closure by returning the value of foreclosed by accessing that property of the locally scoped variable account. Test

borrower()

Declare a function named borrower() that takes 1 argument named loan Test

Write the following statements inside the borrower() function block

  1. Declare a variable named account, with an initial value being a literal object having the following properties and values:
  • key : monthlyIncome, value : 1350
  • key : funds, value : 2800
  • key : loan, value : loan
  1. returns a literal object with the following properties:
  • key : getFunds, value : an unnamed function expression that takes 0 arguments. Test
    • Create a closure by returning the value of funds by accessing that property of the locally scoped variable account. Test
  • key : makePayment, value : an unnamed function expression that takes 0 arguments. Test
    • Conditionally perform either block of statements
      • if account.funds is greater than the value of the loan's monthly payment
        • decrement account.funds by the value of the loan's monthly payment
        • invoke the loan's receivePayment function, and pass in the value of the loan's monthly payment Test
      • otherwise
        • invoke the loan's receivePayment function, and pass in the value of the account's current funds
        • set the value of the funds property of account to 0 Test
  • key : payDay, value : an unnamed function expression that takes 0 arguments. Test
    • Increase the value of the funds property of account by the value of the property monthlyIncome of account Test

Set variables and "step forward in time"

  1. Invoke the loan function and assign it's return value to the variable stevesLoan. Test
  2. Invoke the borrower function passing in the argument stevesLoan and assign it's return value to the variable steve. Test
  3. Create a while loop that runs the following statement while stevesLoan is not foreclosed:
  • steve invokes payDay
  • steve invokes makePayment
  • increment month by 1 Test

Conclusion

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.

Bonus

Adjust the while loop to also exit when the house has been paid off.

License

Copyright (c) 2015 Dev League. Licensed under the MIT license.

About

Closure Exercise

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • JavaScript 100.0%