-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
166 lines (109 loc) Β· 6.62 KB
/
index.js
File metadata and controls
166 lines (109 loc) Β· 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// πππ M V P πππ//
// π‘ 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 principal = 200000;
let interestRate = 0.05;
let years = 30;
// π‘ 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.
Create a variable called `monthlyInterestRate` and give it the value of interest rate divided by 12.
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.
Hint: while these calculations can be done in one line, it might be helpful to create a variable called "numerator" to calculate the numerator, and another called "denominator" to calculate the 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
M = P [ I ( 1 + I )^N ] / [ ( 1 + I )^N β 1 ]
*/
// return p * i * (Math.pow(1 + i, n)) / (Math.pow(1 + i, n) - 1);
let numerator = (principal * (monthlyInterestRate * (Math.pow(1 + monthlyInterestRate, periods))));
let denomenator = (Math.pow(1 + monthlyInterestRate, periods) - 1);
let monthlyRate = numerator / denomenator;
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"
*/
let mortgageCalculator = function(name, monthlyRate) {
var name = 'Donald';
let numerator = (principal * (monthlyInterestRate * (Math.pow(1 + monthlyInterestRate, periods))));
let denomenator = (Math.pow(1 + monthlyInterestRate, periods) - 1);
var monthlyRate = numerator / denomenator;
let sentence = name + ', your monthly rate is ' + monthlyRate;
return sentence;
// var had to be used on name and monthlyRate because an error, has already been declared was passing through let. This solution worked.
}
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
*/
let mortgageCalculators = function(P, I, N) {
var P = 200000;
var I = 0.05 / 12;
var N = years * 12;
var numerator = (P * (I * (Math.pow(1 + I, N))));
var denomenator = (Math.pow(1 + I, N) - 1);
var monthlyRate = numerator / denomenator;
return monthlyRate;
}
console.log(mortgageCalculators());
// π‘ Task 5: Conditionals
/* Add another parameter 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.
*/
creditScore = 425;
if(creditScore < 660) { interestRate += 0.005; }
else if(creditScore > 740) { interestRate -= 0.005; }
else { interestRate += 0; }
console.log('New interest Rate: ' + interestRate);
// π‘ 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"
"{Name}, with an interest rate of 0.035, your monthly rate is $898"
"{Name}, with an interest rate of 0.04, your monthly rate is $955"
"{Name}, with an interest rate of 0.045, your monthly rate is $1013"
"{Name}, with an interest rate of 0.05, your monthly rate is $1074"
"{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"
*/
// function variableInterestRate(principal, interestRate) {
// for(i = 0; i < 11; i++) {
// var principal = 200000;
// var interestRate = 0.015;
// //var years = 30;
// var name = 'Donald';
// var interestRate = interestRate + 0.005;
// var monthlyRate = principal * interestRate;
// console.log(name + ', with an interest rate of ' + interestRate + ', your monthly rate is ', monthlyRate);
// }
// }
function variableInterestRate(principalVal,interestRateVal,yearsVal) {
var principal = principalVal;
var interestRate = interestRateVal;
var years = yearsVal;
var name = 'Donald';
var monthlyInterestRate = interestRate / 12;
var periods = years * 12;
var mRate = (principal * (monthlyInterestRate * (Math.pow(1 + monthlyInterestRate, periods)))) / (Math.pow(1 + monthlyInterestRate, periods) - 1);
var monthlyRate = Math.round(mRate);
for (interestRate = 0.02; interestRate <= 0.06; interestRate += 0.005) {
monthlyRate = monthlyRate + Math.round(monthlyRate * interestRate);
console.log(name + ', with an interest rate of ' + interestRate.toFixed(3) + ' your monthly rate is ' + monthlyRate);
}
}
variableInterestRate(200000,0.02,30);
// πππ 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!) */