diff --git a/index.js b/index.js index 667af155..4b9225c1 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,10 @@ /* Create variables for principal, interest rate, and years. Assign them the values 200000, 0.05, and 30 respectively. Create another value called name and give it the value of your own name. */ - +let principal = 200000; +let interestRate = 0.05; +let years = 30; +let name = 'Nicole'; @@ -15,7 +18,8 @@ (2) Create another variable called `periods` and give it the value of years*12. */ - +let monthlyInterestRate = (interestRate/12); +let periods = (years*12); // 🏡 Task 2: Harder Math @@ -34,16 +38,28 @@ Hint #2: you'll need to use the `math` object for parts of this calculation! When your math is correct, monthlyRate will equal 1073.64 */ - - - +let n1 = Math.pow(1 + monthlyInterestRate,periods); +let numerator = Math.abs (principal*n1*monthlyInterestRate); +let denominator = (n1-1); +let monthlyRate = (numerator/denominator) +console.log(monthlyRate.toFixed(2)) // 🏡 Task 3: Function /* Create a function called `mortgageCalculator` that combines all of the steps from task 1 and 2 and returns a sentence "{Name}, your monthly rate is ${monthlyRate}" If your name is `Oscar` mortgageCalculator() should return "Oscar, your monthly rate is 1073.64" */ - +function mortgageCalculator (name, principal, interestRate, years) { + monthlyInterestRate = (interestRate/12); + periods = (years*12); + n1 = Math.pow(1 + monthlyInterestRate,periods); + numerator = Math.abs(principal*n1*monthlyInterestRate); + denominator = (n1-1); + monthlyRate = (numerator/denominator); + monthlyRate = monthlyRate.toFixed(2); + return console.log (`${name}, your monthly rate is ${monthlyRate}`); + // return console.log(Name + ", your monthly rate is " + monthlyRate + "!") +} @@ -54,8 +70,7 @@ If your name is `Oscar` mortgageCalculator() should return "Oscar, your monthly For example, mortgageCalculator(200000, 0.05, 30); <-- should return 1,073.64 */ - - +// mortgageCalculator("Nicole", 200000, 0.05, 30); @@ -66,8 +81,29 @@ Then, add control flow within your function such that IF creditScore is above 74 Hint: To drop an interest rate by 5% you can take monthlyRate and multiply it by 0.95. Similarly, to increase an interest rate by 5% you'd do monthlyRate * 1.05. */ +function mortgageCalculator (name, principal, interestRate, years, creditScore) { + monthlyInterestRate = (interestRate/12); + periods = (years*12); + n1 = Math.pow(1 + monthlyInterestRate,periods); + numerator = Math.abs(principal*n1*monthlyInterestRate); + denominator = (n1-1); + monthlyRate = (numerator/denominator); + + if (creditScore > 740){ + console.log(`${name}, your monthly rate is ${(monthlyRate*.95).toFixed(2)}`) + } + else if (creditScore < 660){ + console.log(`${name}, your monthly rate is ${(monthlyRate*1.05).toFixed(2)}`); + } + else { + console.log(`${name}, your monthly rate is ${(monthlyRate).toFixed(2)}`); + } + // return console.log (`${name}, your monthly rate is ${monthlyRate}`); +} + +mortgageCalculator ("Nicole" ,200000, 0.05, 30, 670) // 🏡 Task 6: Loops @@ -85,8 +121,21 @@ For example, variableInterestRate(200000, 0.04, 30) should console.log: "{Name}, with an interest rate of 0.055, your monthly rate is $1136" "{Name}, with an interest rate of 0.06, your monthly rate is $1199" */ +let variableInterestRate = function (name, principal, interestRate, years) { + for (let i = interestRate - 0.02; i <= interestRate + 0.02; i = i + 0.005) { + + monthlyInterestRate = (i/12); + periods = (years*12); + n1 = Math.pow(1 + monthlyInterestRate,periods); + numerator = Math.abs(principal*n1*monthlyInterestRate); + denominator = (n1-1); + monthlyRate = (numerator/denominator); + console.log(`${name}, with an interest rate of ${i.toFixed(3)}, your monthly rate is $${Math.round(monthlyRate)}`); + } +} +variableInterestRate ("Nicole", 200000, 0.05, 30) // 🌟🌟🌟 STRETCH 🌟🌟🌟//