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
79 changes: 63 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +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;
const name = "Angelica";

// 🏡 Task 1.5: Simple Math
/* To create a monthly mortgage rate calculator, we need to know the number of years in months and the monthly interest rate.
Expand All @@ -16,8 +17,8 @@ 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 = interestRate/12;
let periods = years*12;

// 🏡 Task 2: Harder Math
/* Create your calculator! Use the formula in the ReadMe to run calculations on your numbers. Save the final value into a variable called monthlyRate.
Expand All @@ -29,18 +30,19 @@ 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 * (numerator / denominator);

// 🏡 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 () {
console.log (Name + ", your monthly rate is $" + monthlyRate);
}

// 🏡 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.
Expand All @@ -49,18 +51,46 @@ For example,
mortgageCalculator(2000000, 0.05, 30); <-- should return 1,073.64
*/




function mortgageCalculator (principal, interestRate, years){

let monthlyInterestRate = interestRate/12;

let periods = years*12;

let numerator = monthlyInterestRate * Math.pow (1 + monthlyInterestRate, periods);

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

let monthlyRate = (principal * (numerator / denominator)).toFixed(2);

console.log (Name + ", your monthly rate is $" + monthlyRate);
}

// 🏡 Task 5: Conditionals
/* Add another paramter to your function called credit score. This parameter will be a number between 0 and 800 (a credit score).

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 mortgageCalculator (principal, interestRate, years, creditScore){
if (creditScore < 660) {
interestRate = interestRate + (interestRate * 0.05);
} else if (creditScore>740) {interestRate = interestRate - (interestRate * 0.05);
} else {interestRate = interestRate
}

let monthlyInterestRate = interestRate/12;

let periods = years*12;

let numerator = monthlyInterestRate * Math.pow (1 + monthlyInterestRate, periods);

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

let monthlyRate = (principal * (numerator / denominator)).toFixed(2);

console.log (Name + ", your monthly rate is $" + monthlyRate);
}

// 🏡 Task 6: Loops
/* Write a new function called variableInterestRate. This function should be the same as mortgageCalculator, except it should console.log the monthly payment for 10 different interest rates at 0.5% increments plus or minus 2% from the inputted interest rate. Complete these calculations using a for loop.
Expand All @@ -78,7 +108,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 (principal, interestRate, years){
let i = interestRate

for (interestRate = interestRate - 0.02; interestRate <= i + 0.02; interestRate = (interestRate+0.005)) {

let monthlyInterestRate = interestRate/12;

let periods = years*12;

let numerator = monthlyInterestRate * Math.pow (1 + monthlyInterestRate, periods);

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

let monthlyRate = (principal * (numerator / denominator)).toFixed(2);

console.log (Name + ", with an interest rate of " + (interestRate).toFixed (3) + " your monthly rate is $" + Math.round(monthlyRate))
}
}


// 🌟🌟🌟 STRETCH 🌟🌟🌟//
Expand Down