Skip to content

Commit

Permalink
can't figure out 6
Browse files Browse the repository at this point in the history
  • Loading branch information
markshares committed May 14, 2020
1 parent d4d5b46 commit 715ffcc
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 26 deletions.
Binary file added .DS_Store
Binary file not shown.
8 changes: 4 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<html>
<head>
<title>JavaScript Foundations</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<p>Open up the console to check your work!</p>
<script src="index.js"></script>

<script src="index.js"></script>
</body>
</html>
99 changes: 77 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

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



*/

let default_principal = 200000;
const default_interest_rate = 0.05;
const default_years = 30;
const default_name = "Mark";

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



var monthlyInterestRate = default_interest_rate / 12;
var periods = default_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 @@ -28,45 +29,91 @@ 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
*/



(default_principal *
(Math.pow(1 + default_interest_rate, periods) * default_interest_rate)) /
(Math.pow(1 + default_interest_rate) - 1);

// 🏑 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() {
var periods = default_years * 12;
var monthlyInterestRate = default_interest_rate / 12;
let numerator =
Math.pow(1 + monthlyInterestRate, periods) * monthlyInterestRate;
let denominator = Math.pow(1 + monthlyInterestRate, periods) - 1;
var payment = default_principal * (numerator / denominator);
return `${default_name}, your monthly payment rate is ${
Math.round(payment * 100) / 100
}`;
}
console.log(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(200000, 0.05, 30); <-- should return 1,073.64
*/




function mortgageCalculatorParams(
principal = default_principal,
interest_rate = default_interest_rate,
years = default_years,
name = default_name
) {
var periods = years * 12;
var monthlyInterestRate = interest_rate / 12;
let numerator =
Math.pow(1 + monthlyInterestRate, periods) * monthlyInterestRate;
let denominator = Math.pow(1 + monthlyInterestRate, periods) - 1;
var payment = principal * (numerator / denominator);
return `${name}, your monthly payment rate is ${
Math.round(payment * 100) / 100
}`;
}
console.log(mortgageCalculatorParams());

// 🏑 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 mortgageCalculatorConditionals(
creditScore,
principal = default_principal,
interest_rate = default_interest_rate,
years = default_years,
name = default_name
) {
if (creditScore > 740) {
interest_rate -= 0.05; //? interest_rate = interest_rate - 0.5
} else if (creditScore < 660) {
interest_rate += 0.05;
}

var periods = years * 12;
var monthlyInterestRate = interest_rate / 12;
let numerator =
Math.pow(1 + monthlyInterestRate, periods) * monthlyInterestRate;
let denominator = Math.pow(1 + monthlyInterestRate, periods) - 1;
var payment = principal * (numerator / denominator);
return `${name}, your monthly payment rate is ${
Math.round(payment * 100) / 100
}`;
}
console.log(mortgageCalculatorConditionals());

// 🏑 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.
For example, variableInterestRate(200000, 0.04, 30) should console.log:
"{Name}, with an interest rate of 0.02, your monthly rate is $739"
"{Name}, with an interest rate of 0.025, your monthly rate is $790"
"{Name}, with an interest rate of 0.03, your monthly rate is $843"
Expand All @@ -78,20 +125,28 @@ 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, interest_rate, years, name) {
var periods = years * 12;
var monthlyInterestRate = interest_rate / 12;
let numerator =
Math.pow(1 + monthlyInterestRate, periods) * monthlyInterestRate;
let denominator = Math.pow(1 + monthlyInterestRate, periods) - 1;
var payment = principal * (numerator / denominator);


var newRate = interest_rate - 0.02 + 0.005;
for (i = 0; i < 10; i++) {
newRate[i];
}
}

// 🌟🌟🌟 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!) */

0 comments on commit 715ffcc

Please sign in to comment.