Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 88 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
/* 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 ir = 0.05;
let years = 30;


// 🏡 Task 1.5: Simple Math
Expand All @@ -16,7 +17,9 @@ Create a variable called `monthlyInterestRate` and give it the value of interest
Create another variable called `periods` and give it the value of years*12.
*/

let monthlyInterestRate = ir / 12;

let periods = years * 12;


// 🏡 Task 2: Harder Math
Expand All @@ -29,7 +32,15 @@ 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 numerator = monthlyInterestRate * Math.pow((1 + monthlyInterestRate),periods);

let denominator = Math.pow(1 + monthlyInterestRate,periods) - 1;

//let monthlyRate = principal * [ monthlyInterestRate * Math.pow( 1 + monthlyInterestRate,periods) ] / [ Math.pow(1 + monthlyInterestRate,periods) - 1 ];

let monthlyRate = principal * (numerator / denominator);

console.log("task 2: " + monthlyRate);


// 🏡 Task 3: Function
Expand All @@ -38,19 +49,37 @@ When your math is correct, monthlyRate will equal 1073.64
If your name is `Oscar` mortgageCalculator() should return "Oscar, your monthly rate is 1073.64"
*/

function mortgageCalculator () {
console.log("Task 3: Daniel, your monthly rate is " + monthlyRate);

}

mortgageCalculator();



// 🏡 Task 4: Arguments and Parameters
/* Substitute the variables in your functions for parameters such that you can substitute `P`, `I`, and `N` when you call the function.

For example,
mortgageCalculator(2000000, 0.05, 30); <-- should return 1,073.64
mortgageCalculator(2000000, 0.05, 30); <-- should return 1,073.64 <-- Noticed one extra 0 in P
*/


//Stuck right now. Is that calculation given above wrong? //////////////////////////////////////////////////

function mortgageCalculator2 (p, ir, n) {

let mIR = (ir / 12); //monthly IR
mperiod = n*12; //converting number of years to number of months

let monthlyRate = p * [ mIR * Math.pow( 1 + mIR,mperiod) ] / [ Math.pow(1 + mIR,mperiod) - 1 ];

return monthlyRate;

}

console.log("task 4: " + mortgageCalculator2(200000, 0.05, 30));


// 🏡 Task 5: Conditionals
Expand All @@ -59,7 +88,30 @@ mortgageCalculator(2000000, 0.05, 30); <-- should return 1,073.64
Then, add control flow within your function such that IF creditScore is above 740, interest rate drops by 0.5%, if credit score is below 660, interest rate increases by 0.5% and if credit score is anywhere between 660 and 740 interest rate doesn't change.
*/

function mortgageCalculator3 (p, ir, n, creditScore) {

let mIR = (ir / 12); //monthly IR
mperiod = n*12; //converting number of years to number of months

let monthlyRate = p * [ mIR * Math.pow( 1 + mIR,mperiod) ] / [ Math.pow(1 + mIR,mperiod) - 1 ];

let newir;

if (creditScore > 740) {

newir = ir - (ir*0.05);

} else if (creditScore<740) {

newir = ir + (ir*0.05);

}

return newir;

}

console.log( "task 5: " + mortgageCalculator3(200000, 0.05, 30, 600));


// 🏡 Task 6: Loops
Expand All @@ -78,7 +130,24 @@ For example, variableInterestRate(200000, 0.04, 30) should console.log:
"{Name}, with an interest rate of 0.06, your monthly rate is $1199"
*/

function variableInterestRate (p, ir, n) {

let i;

let chngIR = 0;

for (i=0; i<9; i++) {


chngIR += 0.005; //Change IR amount

console.log("task 6: Daniel, with an interest rate of " + (ir + chngIR) + " (yearly), your monthly rate is " + mortgageCalculator2(p,(ir+chngIR),n)); //Function inside here returns the monthly rate

}

}

variableInterestRate(200000, 0.04, 30);


// 🌟🌟🌟 STRETCH 🌟🌟🌟//
Expand All @@ -95,3 +164,19 @@ For example, variableInterestRate(200000, 0.04, 30) should console.log:


/* 🏡 Refactor your `variableInterestRate()` function to accept an array of interest rates (make sure to copy and paste as to not lose your work!) */

let rates = [0.05,0.06,0.07,0.08,0.09,0.1]

function variableInterestRate2 (p, ir, n) {

let i;

for (i=0; i<ir.length; i++) {

console.log("Stretch: Daniel, with an interest rate of " + ir[i] + " (yearly), your monthly rate is " + mortgageCalculator2(p,ir[i],n)); //Function inside here returns the monthly rate

}

}

variableInterestRate2(200000, rates, 30);