diff --git a/index.js b/index.js index 667af155..db7246c1 100644 --- a/index.js +++ b/index.js @@ -4,6 +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 +let name = "josh" @@ -15,6 +19,10 @@ (2) Create another variable called `periods` and give it the value of years*12. */ +let monthlyInterestRate = interestRate / 12 +let periods = years * 12 + + @@ -35,8 +43,15 @@ 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 n1 = Math.pow((1 + monthlyInterestRate ) ,periods); +let numerator = principal * n1 * monthlyInterestRate; +let denominator = n1 - 1; +let monthlyRate = numerator / denominator; - +console.log(n1); +console.log(numerator); +console.log(denominator); +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}" @@ -44,9 +59,21 @@ 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 = "josh" + 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() // 🏡 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. @@ -55,6 +82,18 @@ For example, mortgageCalculator(200000, 0.05, 30); <-- should return 1,073.64 */ +function mortgageCalculator(principal, interestRate, years){ + const name = "josh" + 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); @@ -67,8 +106,30 @@ 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 = "josh" + 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) + if (creditScore > 740) + { + interestRate = monthlyRate * 0.95; + } + else if(creditScore < 660) + { + interestRate = monthlyRate * 1.05; + } + else (creditScore <= 740 && creditScore >= 660) + { + interestRate = interestRate; + } +} + +mortgageCalculator(200000, 0.05, 30, 750); // 🏡 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. @@ -85,9 +146,32 @@ For example, variableInterestRate(200000, 0.04, 30) should console.log: "{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 mortgageCalculator(principal, interestRate, years, creditScore) { + const name = "josh" + 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) + if (creditScore > 740) + { + monthlyRate = monthlyRate * 0.95; + } + else if(creditScore < 660) + { + monthlyRate = monthlyRate * 1.05; + } + else (creditScore <= 740 && creditScore >= 660) + { + monthlyRate = monthlyRate; + } +} + +mortgageCalculator(200000, 0.05, 30, 750); + +for (monthlyInterestRate = .02; monthlyInterestRate <=0.06; monthlyInterestRate += 0.05) // 🌟🌟🌟 STRETCH 🌟🌟🌟//