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
102 changes: 77 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,114 @@

// 🏡 Task 1: Variables
/* 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.
*/



*/

var principle = 200000;
var interestRate = 0.05;
var years = 30;
var name = "Oscar";

// 🏡 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.
/* To create a monthly mortgage rate calculator, we need to know the number of years in months and the monthly interest rate. */

(1) Create a variable called `monthlyInterestRate` and give it the value of interest rate divided by 12.
/*(1) Create a variable called `monthlyInterestRate` and give it the value of interest rate divided by 12.
(2) Create another variable called `periods` and give it the value of years*12.
*/
var monthlyInterestRate = 0.05 / 12;
// console.log(monthlyInterestRate);

var periods = years * 12;
// console.log(periods);


// ^periods is your N

// 🏡 Task 2: Harder Math
/* Create your calculator! Use the formula in the ReadMe (also below) to run calculations on your numbers. Save the final value into a variable called monthlyRate.

M = P [ I ( 1 + I )^N ] / [ ( 1 + I )^N – 1 ]

P = principal
I = interestRate
N = periods

Hint: while these calculations can be done in one line, it might be helpful to create seperate variables to hold parts of your equation. That might look like this:

(1) Create a variable called n1 and set it equal to (1 + monthlyInterestRate )^N

(2) Create a variable called numerator and set it equal to p * n1 * monthlyInterestRate

(3) Create a variable called denominator and set it equal to n1 - 1

(4) Create a variable called monthlyRate and set it equal to numerator/denominator

let monthlyRate = numerator % denominator

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
*/



var n1 = Math.pow(1 + monthlyInterestRate, periods);
// console.log(n1); 1.00416667
var numerator = principle * n1 * monthlyInterestRate;
// console.log(numerator);
var denominator = n1 - 1;
// console.log(denominator);
var monthlyRate = (numerator / denominator).toFixed(2);
// console.log(monthlyRate);

// 🏡 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);
}

mortgageCalculator();


// ^^This is needed to run the function

// 🏡 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(200000, 0.05, 30); <-- should return 1,073.64
*/




var p = parseInt("");
var i = parseInt("");
var n = parseInt("");

function mortgageCalculator2(p, i, n) {
var n = n * 12;
var n2 = Math.pow(1 + i / 12, n);
var numerator2 = p * n2 * (i / 12);
var denominator2 = n2 - 1;
var monthlyRate2 = (numerator2 / denominator2).toFixed(2);
return name + ", your monthly rate is " + monthlyRate;
}
mortgageCalculator2();

// 🏡 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.
/* Add another paramter to your function called credit score. This parameter will be a number between 0 and 800 (a credit score).*/

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.
*/
/*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.*/

/*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.
*/

var creditScore = parseInt("");

if (creditScore > 740) {
monthlyRate = (monthlyRate * 0.95).toFixed(2);
} else if (creditScore < 660) {
monthlyRate = (monthlyRate * 1.05).toFixed(2);
} else if (creditScore > 660 && creditScore < 740) {
monthlyRate = (monthlyRate * 1).toFixed(2);
}

// expected output: "NOT positive"
// 🏡 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 @@ -86,20 +126,32 @@ For example, variableInterestRate(200000, 0.04, 30) should console.log:
"{Name}, with an interest rate of 0.06, your monthly rate is $1199"
*/



let variableInterestRate = function (p, i, n) {
for (let x = i - 0.02; x < i + 0.02; x += 0.005) {
let principal = p;
let rate = x.toFixed(3);
let interestRate = rate * 1;
let years = n;
let periods = years * 12;
let n1 = Math.pow(1 + monthlyInterestRate, periods);
let numerator = principal * n1 * monthlyInterestRate;
let denominator = n1 - 1;
let monthlyRate = numerator / denominator;
let result = Number(Math.round(monthlyRate * 100) / 100).toFixed(0);
return result;
}
};
var variableResult = Number(variableInterestRate(200000, 0.04, 30));
console.log(variableResult);

// 🌟🌟🌟 STRETCH 🌟🌟🌟//

/* Attempt any of the stretch goals below once you have finished the work above. Remember as always, these may require additional research beyond what you learned today */

/* 🏡 Add `Property Tax`, `Homeowner's insurance` and `HOA fees` as parameters in your function to calculate total monthly spending on housing */


/* 🏡 Build a calculator function that accepts `monthly payment` and `interest rate` and returns the maximum loan that a person could afford */


/* 🏡 Explore using `window.prompt()` to allow a user to input parameters in the browser */


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