diff --git a/README.md b/README.md index 172618c7..bf766cfa 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ As you work on your code you should make use of `console.log` to check your prog ### Task 2b: Exit Ticket -Once you begin, you will have 15 minutes to answer the questions [here](https://app.codesignal.com/public-test/pi7Q49mgrZbodiRzM/aXzit9h4ZRp3Si) +- [x] Once you begin, you will have 15 minutes to answer the questions [here](https://app.codesignal.com/public-test/pi7Q49mgrZbodiRzM/aXzit9h4ZRp3Si) The completion of these questions is mandatory for MVP. However, passing the quiz doesn't affect your standing as a Lambda School student whatsoever. This is Lambda School testing itself! Please answer honestly and to the best of your ability without using external references. @@ -65,18 +65,18 @@ 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 -- [ ] 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!) +- [x] Add `Property Tax`, `Homeowner's insurance` and `HOA fees` as parameters in your function to calculate total monthly spending on housing +- [x] Build a calculator function that accepts `monthly payment` and `interest rate` and returns the maximum loan that a person could afford +- [x] Explore using `window.prompt()` to allow a user to input parameters in the browser +- [x] Refactor your `variableInterestRate()` function to accept an array of interest rates (make sure to copy and paste as to not lose your work!) ## Submission format 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** -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 +1. [x] Submit a pull request to merge `` branch into master. **Please don't merge your own pull request** +2. [x] Add your TL as a reviewer on the pull-request +3. [x] Your TL will count the project as complete by merging the branch back into master ## Resources diff --git a/index.html b/index.html index b458c1ff..a43d49cd 100644 --- a/index.html +++ b/index.html @@ -4,10 +4,57 @@ JavaScript Foundations + + -

Open up the console to check your work!

+ + +

Want to know your mortgage?

- +
+ +
+ + +

+ + + + + + diff --git a/index.js b/index.js index 667af155..9345cfb4 100644 --- a/index.js +++ b/index.js @@ -3,10 +3,10 @@ // 🏡 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; +let name = "Rhiannon"; // 🏡 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. @@ -14,9 +14,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; // 🏡 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. @@ -35,18 +34,37 @@ 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; // 🏡 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(name, principal) { + + let interestRate = 0.05; + let years = 30; + + let monthlyInterestRate = interestRate / 12; + let periods = years * 12; + let n1 = Math.pow((1 + monthlyInterestRate),periods); + let numerator = principal * n1 * monthlyInterestRate; + let denominator = n1 - 1; + let monthlyRate = numerator / denominator; + monthlyRate = monthlyRate.toFixed(2); + return `${name}, your monthly rate is ${monthlyRate}.` +} +// Test Case +var output = mortgageCalculator("Oscar", 200000); +console.log(output); // 🏡 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,11 +73,27 @@ For example, mortgageCalculator(200000, 0.05, 30); <-- should return 1,073.64 */ +function mortgageCalculator(name, principal, interestRate, years) { + let monthlyInterestRate = interestRate / 12; + let periods = years * 12; + let n1 = Math.pow((1 + monthlyInterestRate),periods); + let numerator = principal * n1 * monthlyInterestRate; + let denominator = n1 - 1; + let monthlyRate = numerator / denominator; + monthlyRate = monthlyRate.toFixed(2); + return `${name}, your monthly rate is ${monthlyRate}.` +} -// 🏡 Task 5: Conditionals +// TEST CASE +var output = mortgageCalculator("Oscar", 200000, 0.05, 30); +console.log(output); +// <-- should return 1,073.64 + + +// 🏡 Task 5: Conditionals TYPO HERE: 0.5% should be 5% /* 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. @@ -67,8 +101,32 @@ 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(name, principal, interestRate, years, creditScore) { + let monthlyInterestRate = interestRate / 12; + let periods = years * 12; + let newInterestRate = 0; + + if (creditScore > 740) { + monthlyInterestRate = monthlyInterestRate * .95; + } else if (creditScore < 600) { + monthlyInterestRate = monthlyInterestRate * 1.05; + } else if (creditScore >= 660 && creditScore <= 740) { + newInterestRate = interestRate; + } + + let n1 = Math.pow((1 + monthlyInterestRate),periods); + let numerator = principal * n1 * monthlyInterestRate; + let denominator = n1 - 1; + let monthlyRate = numerator / denominator; + monthlyRate = monthlyRate.toFixed(2); + + return `${name}, your monthly rate is ${monthlyRate}.` +} + +// TEST CASE +var output = mortgageCalculator("Rhiannon", 200000, 0.05, 30, 665); +console.log(output); +// <-- should return 1,073.64 // 🏡 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. @@ -87,6 +145,64 @@ For example, variableInterestRate(200000, 0.04, 30) should console.log: */ +function mortgageCalculator(name, principal, interestRate, years, creditScore) { + let monthlyInterestRate = interestRate / 12; + let periods = years * 12; + let newInterestRate = 0; + + if (creditScore > 740) { + monthlyInterestRate = monthlyInterestRate * .95; + } else if (creditScore < 600) { + monthlyInterestRate = monthlyInterestRate * 1.05; + } else if (creditScore >= 660 && creditScore <= 740) { + newInterestRate = interestRate; + } + + let n1 = Math.pow((1 + monthlyInterestRate),periods); + let numerator = principal * n1 * monthlyInterestRate; + console.log(numerator); + let denominator = n1 - 1; + + if (denominator === 0) { // edge case for zero, denominator cannot be 0 + return undefined; + } + return Math.round(numerator / denominator); +} + + +function variableInterestRate(name, principal, interestRate, years, creditScore) { + + let variableInterest = interestRate - .02; + + for (let i = 0; i < 10; i += 1) { + + let monthlyRate = mortgageCalculator(name, principal, variableInterest, years, creditScore); + + let roundedVarInt = variableInterest.toFixed(3); + + console.log(`${name}, with an interest rate of ${roundedVarInt}, your montly rate is $${monthlyRate}.`); + + variableInterest += .005; + } +} + +// TEST CASE +// var output = variableInterestRate("Rhiannon", 200000, 0.04, 30, 655); +// console.log(output); +/* +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" +*/ + // 🌟🌟🌟 STRETCH 🌟🌟🌟// @@ -94,12 +210,133 @@ For example, variableInterestRate(200000, 0.04, 30) should console.log: /* 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 */ - +function totalMonthlyHousingExpense(principal, interestRate, years, creditScore, propertyTax, homeownersInsurance, hoa) { + let monthlyInterestRate = interestRate / 12; + let periods = years * 12; + let newInterestRate = 0; + + if (creditScore > 740) { + monthlyInterestRate = monthlyInterestRate * .95; + } else if (creditScore < 600) { + monthlyInterestRate = monthlyInterestRate * 1.05; + } else if (creditScore >= 660 && creditScore <= 740) { + newInterestRate = interestRate; + } + + let n1 = Math.pow((1 + monthlyInterestRate),periods); + let numerator = principal * n1 * monthlyInterestRate; + let denominator = n1 - 1; + + if (denominator === 0) { // edge case for zero, denominator cannot be 0 + return undefined; + } + + let monthlyPayment = Math.round(numerator / denominator); + let totalMonthlyHousingExpense = monthlyPayment + propertyTax + homeownersInsurance + hoa; + return totalMonthlyHousingExpense; +} + +// SAMPLE CODE IF I WANTED AN INDEPENDENT FUNCTION FOR: +// function monthlyHomeownersInsuranceCalculator(principal) { +// let num = principal / 100000; +// return num * 35; +// } + +// function monthlyPropertyTaxCalculator(principal) { +// let yearlyTax = principal * .00953; +// let monthlyTax = yearlyTax / 12; +// return monthlyTax; +// } +// function totalMonthlyHousingExpense(principal, interestRate, years, creditScore) { +// let hoa = 300; +// let monthlyHousingExpense = monthlyPropertyTaxCalculator(principal) + hoa + monthlyHomeownersInsuranceCalculator(principal) + mortgageCalculator(principal, interestRate, years, creditScore); +// monthlyHousingExpense = Math.round(monthlyHousingExpense); +// return `Your monthly housing expense will be $${monthlyHousingExpense}.` +// } + + +//TEST CASE +var output = totalMonthlyHousingExpense(200000, 0.04, 30, 655, 65, 200, 200); +console.log(output); /* 🏡 Build a calculator function that accepts `monthly payment` and `interest rate` and returns the maximum loan that a person could afford */ - +function maxLoan(monthlyPayment, interestRate) { + + let possiblePrinciple = 0; + let monthlyRate = 0; + + do { + + let monthlyRate = mortgageCalculator(possiblePrinciple, interestRate); + console.log(monthlyRate); + if (monthlyRate >= monthlyPayment) { + return `Your max loan is ${possiblePrinciple}.`; + } else { + possiblePrinciple += 1; + } + } while (monthlyRate < monthlyPayment); +} + +function mortgageCalculator(principal, interestRate) { + let years = 30; + let monthlyInterestRate = interestRate / 12; + let periods = years * 12; + + let n1 = Math.pow((1 + monthlyInterestRate),periods); + let numerator = principal * n1 * monthlyInterestRate; + let denominator = n1 - 1; + let monthlyRate = numerator / denominator; + monthlyRate = monthlyRate.toFixed(2); + + return monthlyRate; +} + +// TEST CASE +var output = maxLoan(200, 0.05); +console.log(output); /* 🏡 Explore using `window.prompt()` to allow a user to input parameters in the browser */ +// COMPLETE: Website made, buttons styled, mortgage calculator in place. + /* 🏡 Refactor your `variableInterestRate()` function to accept an array of interest rates (make sure to copy and paste as to not lose your work!) */ +function variableInterestRate(array) { + + // let arrayOfInterestRates = array + // arrayOfInterestsRates.forEach(mortgageCalculator); + + array.forEach(mortgageCalculator); +} + +// TEST CODE +output = variableInterestRate([0.4, 0.5, 0.6]); +console.log(output); + +function mortgageCalculator(interestRate) { + let monthlyInterestRate = interestRate / 12; + let years = 30; + let periods = years * 12; + let newInterestRate = 0; + let creditScore = 670; + let principal = 200000; + + if (creditScore > 740) { + monthlyInterestRate = monthlyInterestRate * .95; + } else if (creditScore < 600) { + monthlyInterestRate = monthlyInterestRate * 1.05; + } else if (creditScore >= 660 && creditScore <= 740) { + newInterestRate = interestRate; + } + + let n1 = Math.pow((1 + monthlyInterestRate),periods); + let numerator = principal * n1 * monthlyInterestRate; + let denominator = n1 - 1; + + if (denominator === 0) { // edge case for zero, denominator cannot be 0 + return undefined; + } + monthlyRate = Math.round(numerator / denominator); + + console.log(`With an interest rate of ${interestRate}, your montly rate is $${monthlyRate}.`); +} diff --git a/styles.css b/styles.css new file mode 100644 index 00000000..b6ccda26 --- /dev/null +++ b/styles.css @@ -0,0 +1,95 @@ +html, body, div, span, applet, object, iframe, +h1, h2, h3, h4, h5, h6, p, blockquote, pre, +a, abbr, acronym, address, big, cite, code, +del, dfn, em, img, ins, kbd, q, s, samp, +small, strike, strong, sub, sup, tt, var, +b, u, i, center, +dl, dt, dd, ol, ul, li, +fieldset, form, label, legend, +table, caption, tbody, tfoot, thead, tr, th, td, +article, aside, canvas, details, embed, +figure, figcaption, footer, header, hgroup, +menu, nav, output, ruby, section, summary, +time, mark, audio, video { + margin: 0; + padding: 0; + border: 0; + font-size: 100%; + font: inherit; + vertical-align: baseline; +} + + + +body { + background-image: url(https://cdn.pixabay.com/photo/2018/08/17/12/53/halloween-3612815_1280.png); + background-position: center; + background-color: black; + background-size: contain; + background-repeat: no-repeat; + width: 100%; + height: 200vh; + display: flex; + /* justify-content: space-between; */ + padding: 400px, 400px; + flex-flow: column nowrap; + align-content: center; + box-sizing: border-box; +} + +.introText { + font-size: 4.5rem; + color: red; + font-family: creepster; + display: flex; + flex-flow: row nowrap; + height: 25%; + margin: 1em, auto; + align-self: center; + padding: 20px; +} + +.container2 { + width: 100%; + height: 30%; + display: flex; + flex-flow: column nowrap; + padding: 2rem; + +} + +.mortgageButton { + font-family: Creepster, cursive; + color: rgb(247, 216, 60); + font-size: 3rem; + background-color: rgb(54, 2, 83); + padding: 200px, 200px; + width: 5em; + height: 5em; + border-radius: 180px; + align-self: flex-start; + margin-left: 250px; + box-shadow: 0px 0px 60px 5px red; +} + +.mortgageButton:focus { + outline: none; + box-shadow: none; +} + + + + + + +#result { + font-size: 3em; + color: red; + font-family: creepster; + display: flex; + flex-flow: column wrap; + width: 100%; + height: 35%; + align-self: center; + padding-left: 80%; +} \ No newline at end of file