From 9aa0aa30cd1be193891623c368e8362b6407f516 Mon Sep 17 00:00:00 2001 From: Jeremiah Trnka Date: Mon, 8 Jun 2020 22:03:13 -0700 Subject: [PATCH 1/3] Updated index.js file with solutions. --- .vscode/launch.json | 20 ++++++++ .vscode/settings.json | 5 ++ index.js | 106 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 121 insertions(+), 10 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..0cd27191 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,20 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + + + + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${file}" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..1c354ea0 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "editor.wordWrap": "on", + "editor.fontSize": 14, + "editor.fontWeight": "500" +} \ No newline at end of file diff --git a/index.js b/index.js index 55e48136..7c5b0de0 100644 --- a/index.js +++ b/index.js @@ -4,8 +4,13 @@ /* 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 int = 0.05; +let years = 30; + +let name = 'Jeremiah'; // 🏡 Task 1.5: Simple Math @@ -15,8 +20,9 @@ (2) Create another variable called `periods` and give it the value of years*12. */ +let monthlyInterestRate = int/12; - +let periods = years*12; // 🏡 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. @@ -30,13 +36,22 @@ Hint: while these calculations can be done in one line, it might be helpful to c (3) Create a variable called numerator and set it equal to n1 * n2 (4) Create a variable called denominator and set it equal to n1 - 1 (5) Create a variable called monthlyRate and set it equal to numerator/denominator +*/ -Hint #2: you'll need to use the `math` object for parts of this calculation! +// let n1 = Math.pow((1 + monthlyInterestRate),periods); +// let n2 = n1 * monthlyInterestRate; +// let numerator = n1 * n2; +// let denominator = n1 - 1; +// let monthlyRate = numerator/denominator; +// console.log(monthlyRate.toFixed(2)); //answers 0.02 -When your math is correct, monthlyRate will equal 1073.64 -*/ +let monthlyRate = (principal * monthlyInterestRate) / (1 - (Math.pow((1 + monthlyInterestRate),periods * -1))); +console.log(monthlyRate.toFixed(2)); +/* 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 +*/ // 🏡 Task 3: Function @@ -45,9 +60,18 @@ 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 mortgageCalculatorName(P,I,N,Name) { + let principal = P; + let int = I; + let years = N; + let monthlyInterestRate = int/12; + let periods = years*12; + let monthlyRate = (principal * monthlyInterestRate) / (1 - (Math.pow((1 + monthlyInterestRate),periods * -1))); + // let message = Name + ", your monthly rate is " + monthlyRate.toFixed(2); + console.log(Name + ", your monthly rate is " + monthlyRate.toFixed(2)); + // return message; +} +mortgageCalculatorName(200000,0.05,30,'Oscar'); // 🏡 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. @@ -56,7 +80,18 @@ For example, mortgageCalculator(200000, 0.05, 30); <-- should return 1,073.64 */ +function mortgageCalculator(P,I,N) { + let principal = P; + let int = I; + let years = N; + let monthlyInterestRate = int/12; + let periods = years*12; + let monthlyRate = (principal * monthlyInterestRate) / (1 - (Math.pow((1 + monthlyInterestRate),periods * -1))); + console.log(monthlyRate); + return monthlyRate; +} +mortgageCalculator(200000,0.05,30); @@ -68,7 +103,25 @@ 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(P,I,N,C) { + let interest; + if (C > 739) { + interest = I - 0.005; + } else if (C < 660) { + interest = I + 0.005; + } else { + interest = I; + } + let principal = P; + let years = N; + let monthlyInterestRate = interest/12; + let periods = years*12; + let monthlyRate = (principal * monthlyInterestRate) / (1 - (Math.pow((1 + monthlyInterestRate),periods * -1))); + // return monthlyRate.toFixed(2); + console.log("Your monthly mortgage payment is " + monthlyRate.toFixed(2)); + } + +mortgageCalculator(200000,0.05,30,700); // 🏡 Task 6: Loops @@ -87,8 +140,23 @@ 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(P, I, N) { + let interest = I - 0.025; + let count = 0; + for (let i = 0; i < 10; i++) { + interest = interest + .005; + // console.log(interest.toFixed(3)); + count = count + 1; + let principal = P; + let years = N; + let monthlyInterestRate = interest/12; + let periods = years*12; + let monthlyRate = (principal * monthlyInterestRate) / (1 - (Math.pow((1 + monthlyInterestRate),periods * -1))); + console.log("{Name}, with an interest rate of " + interest.toFixed(3) + ", your monthly rate is $" + monthlyRate.toFixed(0)); + } +} + +variableInterestRate(200000,0.04,30); // 🌟🌟🌟 STRETCH 🌟🌟🌟// @@ -96,6 +164,24 @@ For example, variableInterestRate(200000, 0.04, 30) should console.log: /* 🏡 Add `Property Tax`, `Homeowner's insurance` and `HOA fees` as parameters in your function to calculate total monthly spending on housing */ +function mortgageCalculatorComplete(P,I,N,T,S,H) { + let principal = P; + let interest = I; + let years = N; + let monthlyInterestRate = interest/12; + let periods = years*12; + let propTax = T/12; + let insurance = S/12; + let hoaFees = H/12; + let monthlyRate = (principal * monthlyInterestRate) / (1 - (Math.pow((1 + monthlyInterestRate),periods * -1))); + let totalMonthlyCost = monthlyRate + propTax + insurance + hoaFees; + console.log("Your monthly mortgage payment is " + monthlyRate.toFixed(2)); + console.log("Your total monthly housing cost, including property tax, insurance and HOA fees, is " + totalMonthlyCost.toFixed(2)); + // return monthlyRate; +} + +mortgageCalculatorComplete(200000,0.05,30,1800,1200,2400); + /* 🏡 Build a calculator function that accepts `monthly payment` and `interest rate` and returns the maximum loan that a person could afford */ From 42770daa3ea614bc4ccb32f71f19a13630b892b0 Mon Sep 17 00:00:00 2001 From: Jeremiah Trnka Date: Tue, 9 Jun 2020 12:36:43 -0700 Subject: [PATCH 2/3] Updated index.js and readme. --- README.md | 4 ++-- index.js | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 172618c7..f650af87 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ The completion of these questions is mandatory for MVP. However, passing the qui After you have completed the requirements, try any of the following challenges. As always, note that these may require additional research beyond what you learned in this module. -- [ ] Add `Property Tax`, `Homeowner's insurance` and `HOA fees` as parameters in your function to calculate total monthly spending on housing +- [x] 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!) @@ -74,7 +74,7 @@ After you have completed the requirements, try any of the following challenges. Follow these steps for completing your project. -1. [ ] Submit a pull request to merge `` branch into master. **Please don't merge your own pull request** +1. [x] Submit a pull request to merge `` branch into master. **Please don't merge your own pull request** 2. [ ] Add your TL as a reviewer on the pull-request 3. [ ] Your TL will count the project as complete by merging the branch back into master diff --git a/index.js b/index.js index 7c5b0de0..47e3159b 100644 --- a/index.js +++ b/index.js @@ -38,15 +38,15 @@ Hint: while these calculations can be done in one line, it might be helpful to c (5) Create a variable called monthlyRate and set it equal to numerator/denominator */ -// let n1 = Math.pow((1 + monthlyInterestRate),periods); -// let n2 = n1 * monthlyInterestRate; -// let numerator = n1 * n2; -// let denominator = n1 - 1; -// let monthlyRate = numerator/denominator; -// console.log(monthlyRate.toFixed(2)); //answers 0.02 +let n1 = Math.pow((1 + monthlyInterestRate),periods); +// let n2 = n1 * monthlyInterestRate; // remove step 3 above +let numerator = n1 * monthlyInterestRate; // use monthlyInterestRate instead of n2 in step 3 above +let denominator = n1 - 1; +let monthlyRate = principal * (numerator/denominator); // add principal * (numerator/denominator) to step 5 above +console.log(monthlyRate.toFixed(2)); //answers 0.02 -- resolves to 1073.64 with above modifications to instructions. -let monthlyRate = (principal * monthlyInterestRate) / (1 - (Math.pow((1 + monthlyInterestRate),periods * -1))); -console.log(monthlyRate.toFixed(2)); +// let monthlyRate = (principal * monthlyInterestRate) / (1 - (Math.pow((1 + monthlyInterestRate),periods * -1))); +// console.log(monthlyRate.toFixed(2)); /* Hint #2: you'll need to use the `math` object for parts of this calculation! @@ -175,8 +175,8 @@ function mortgageCalculatorComplete(P,I,N,T,S,H) { let hoaFees = H/12; let monthlyRate = (principal * monthlyInterestRate) / (1 - (Math.pow((1 + monthlyInterestRate),periods * -1))); let totalMonthlyCost = monthlyRate + propTax + insurance + hoaFees; - console.log("Your monthly mortgage payment is " + monthlyRate.toFixed(2)); - console.log("Your total monthly housing cost, including property tax, insurance and HOA fees, is " + totalMonthlyCost.toFixed(2)); + console.log("Your monthly mortgage payment is $" + monthlyRate.toFixed(2)); + console.log("Your total monthly housing cost, including property tax, insurance and HOA fees, is $" + totalMonthlyCost.toFixed(2)); // return monthlyRate; } From 56285750fabb080dd40f4732d312cdf93309a371 Mon Sep 17 00:00:00 2001 From: Jeremiah Trnka Date: Tue, 9 Jun 2020 12:56:43 -0700 Subject: [PATCH 3/3] Updated index.js and readme. --- README.md | 2 +- index.js | 26 ++++++++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f650af87..5369cf5a 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ After you have completed the requirements, try any of the following challenges. Follow these steps for completing your project. -1. [x] Submit a pull request to merge `` branch into master. **Please don't merge your own pull request** +1. [x] Submit a pull request to merge `` branch into master. **Please don't merge your own pull request** (https://github.com/kc0buk/JavaScript-Foundations/pull/1) 2. [ ] Add your TL as a reviewer on the pull-request 3. [ ] Your TL will count the project as complete by merging the branch back into master diff --git a/index.js b/index.js index 47e3159b..d8caec5d 100644 --- a/index.js +++ b/index.js @@ -140,8 +140,15 @@ 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(P, I, N) { - let interest = I - 0.025; +function variableInterestRate(P, I, N, C) { + let interest; + if (C > 739) { + interest = I - 0.03; + } else if (C < 660) { + interest = I - 0.02; + } else { + interest = I - 0.025; + } let count = 0; for (let i = 0; i < 10; i++) { interest = interest + .005; @@ -156,7 +163,7 @@ function variableInterestRate(P, I, N) { } } -variableInterestRate(200000,0.04,30); +variableInterestRate(200000,0.04,30,700); // Input -- P: principal, I: interest, N: years, C: credit score // 🌟🌟🌟 STRETCH 🌟🌟🌟// @@ -164,9 +171,16 @@ variableInterestRate(200000,0.04,30); /* 🏡 Add `Property Tax`, `Homeowner's insurance` and `HOA fees` as parameters in your function to calculate total monthly spending on housing */ -function mortgageCalculatorComplete(P,I,N,T,S,H) { +function mortgageCalculatorComplete(P,I,N,C,T,S,H) { let principal = P; - let interest = I; + let interest; + if (C > 739) { + interest = I - 0.005; + } else if (C < 660) { + interest = I + 0.005; + } else { + interest = I; + } let years = N; let monthlyInterestRate = interest/12; let periods = years*12; @@ -180,7 +194,7 @@ function mortgageCalculatorComplete(P,I,N,T,S,H) { // return monthlyRate; } -mortgageCalculatorComplete(200000,0.05,30,1800,1200,2400); +mortgageCalculatorComplete(200000,0.05,30,700,1800,1200,2400); /* Input -- P: principal, I: interest, N: years, C: credit score, T: property taxes (annually), S: homeowner's insurance (annually), H: HOA fees (anually) */ /* 🏡 Build a calculator function that accepts `monthly payment` and `interest rate` and returns the maximum loan that a person could afford */