Question: JAVAscript Declare a function named calcFutureValue which accepts three arguments: principal , rate , and years . Inside the function if the principal value is

 JAVAscript Declare a function named calcFutureValue which accepts three arguments: principal, JAVAscript

  1. Declare a function named calcFutureValue which accepts three arguments: principal, rate, and years. Inside the function if the principal value is less than or equal to zero, throw an error like this: new Error("Principal value must be greater than zero"). If the rate value is less than or equal to zero, throw an error: new Error("Rate value must be greater than zero"). If the years value is less than or equal to zero, throw an error: new Error("Years value must be greater than zero").

  1. Checking the argument values first within a function is good coding practice. You do not want the function to perform calculations on invalid amounts. Now that the function arguments have been properly vetted, the function can proceed to do its work. Next:
  • Declare a variable monthlyRate using var and assign to it the expression rate divided by twelve divided by 100.
  • Declare a variable months using var and assign to it the expression years multiplied by twelve.
  • Declare a variable futureValue using var and assign to it the value of zero
  • Define an iteration loop that will loop months number of times and inside the body of iteration loop assign to futureValue the expression of (futureValue + principal) times (one plus monthlyRate)
  • Return the futureValue fixed to two decimal places.

  1. Now we can test the function calcFutureValue by passing values in to the function.
  • Declare a variable investment and assign to it the value of 10.
  • Declare a variable annualRate and assign to it the value of 4;
  • Declare a variable years and assign to it the value of 5;
  • Call the function and pass in the values.
  • Display the results to the console (console.log(calcFutureValue(investment, annualRate, years));

Part 2 Question 1 The future value is: 665.20 The future value is: 6828.94 ERROR: Principal must be more than 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!