diff --git a/index.js b/index.js index 667af155..d0501d09 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,11 @@ // 🏡 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; +const name = 'Jessica'; @@ -14,6 +18,8 @@ (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. */ +let monthlyInterestRate = interestRate/12; +let periods = years*12; @@ -35,7 +41,12 @@ 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 */ +const n1 = Math.pow((1 + monthlyInterestRate ) ,periods); +const numerator = principal * n1 * monthlyInterestRate; +const denominator = n1 - 1; +let monthlyRate = numerator/denominator; +console.log("$" + monthlyRate); // 🏡 Task 3: Function @@ -44,6 +55,23 @@ 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() +// { +// let principal = 200000; +// let interestRate = 0.05; +// let years = 30; +// const name = "Jessica"; +// let monthlyInterestRate = interestRate/12; +// let periods = years*12; +// const n1 = Math.pow((1 + monthlyInterestRate), periods); +// const numerator = principal * n1 * monthlyInterestRate; +// const denominator = n1 - 1; +// let monthlyRate = numerator / denominator; +// console.log(name + ", your monthly rate is " + monthlyRate); +// } + +// mortgageCalculator(); + @@ -54,10 +82,18 @@ If your name is `Oscar` mortgageCalculator() should return "Oscar, your monthly For example, mortgageCalculator(200000, 0.05, 30); <-- should return 1,073.64 */ - - - - +// function mortgageCalculator(principal, interestRate, years){ +// const name = "Jessica"; +// let monthlyInterestRate = interestRate/12; +// let periods = years*12; +// const n1 = Math.pow((1 + monthlyInterestRate), periods); +// const numerator = principal * n1 * monthlyInterestRate; +// const denominator = n1 - 1; +// let monthlyRate = numerator / denominator; +// console.log(name + ", your monthly rate is " + monthlyRate); +// } + +// mortgageCalculator(200000, 0.05, 30); // 🏡 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). @@ -66,7 +102,27 @@ Then, add control flow within your function such that IF creditScore is above 74 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. */ +// function mortgageCalculator(principal, interestRate, years, creditScore){ +// const name = "Jessica"; +// let monthlyInterestRate = interestRate/12; +// let periods = years*12; +// const n1 = Math.pow((1 + monthlyInterestRate), periods); +// const numerator = principal * n1 * monthlyInterestRate; +// const denominator = n1 - 1; +// let monthlyRate = numerator / denominator; +// if (creditScore > 740){ +// monthlyRate = (monthlyRate * 0.95); +// } else if (creditScore < 660){ +// monthlyRate = (monthlyRate * 1.05); +// } else if (creditScore <= 740 && creditScore >= 660){ +// monthlyRate = (monthlyRate); +// } + +// console.log(name + ", your monthly rate is " + monthlyRate); +// } + +// mortgageCalculator(200000, 0.05, 30, 750); @@ -86,6 +142,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){ + const name = "Jessica"; + const count= 10; + let monthlyInterestRate = interestRate/12; + let periods = years*12; + const n1 = (1 + monthlyInterestRate) ** periods; + const numerator = principal * n1 * monthlyInterestRate; + const denominator = n1 - 1; + let monthlyRate = numerator / denominator; + + + for (let interestRate = .02; interestRate <= .06; interestRate += .005) { + let monthlyRate = principal * interestRate / 12 * (Math.pow(1 + interestRate / 12, years * 12)) / (Math.pow(1 + interestRate / 12, years * 12) - 1); + console.log(name + ", with an interest rate of " + interestRate.toFixed(3) + ", your monthly interest rate is $" + Math.round(monthlyRate)); + } + +} +variableInterestRate(200000, 0.04, 30);