Home > Enterprise >  How do I take inputs through HTML and then use those to run a loop with javascript?
How do I take inputs through HTML and then use those to run a loop with javascript?

Time:01-30

I am trying to write a code which will calculate compound interest earned on an investment and after each year (i) ends, it will ask the user if they would like to continue staying invested or if they would like to cash out.

I have been able to write a for loop for calculating final payment after 10 years. However, I can't seem to figure out how to break the loop after each year ends and then ask the user a simple yes or no question about their investment preference. This is the code I input to calculate interest:

let userInput = false;
let principal = 100000;
let rate = 0.05;
let amount;

for(let year = 1; year <= 10;   year)
{
if(userInput) {
    amount = principal * Math.pow(1.0   rate, year);
    console.log(year);
    console.log(amount.toFixed(2));
}
else {
    {break;}
}
}

I am guessing I'll need to create a form with HTML as well, to get the input but not sure how to then take that input and add it into my java code.

I calculated compound interest but can't seem to break the loop and ask the user for go ahead after each year ends.

CodePudding user response:

You could do something like this. It's a loop that asks the user if they would like to continue staying invested or cash out after each year

function calculateInterest() {
  var principal = document.getElementById("principal").value;
  var rate = document.getElementById("rate").value / 100;
  var years = document.getElementById("years").value;
  
  for (var i = 1; i <= years; i  ) {
    var interest = principal * rate;
    principal  = interest;
    console.log("Year "   i   ": $"   principal.toFixed(2));
    
    var continueInvesting = prompt("Do you want to continue investing for the next year? (yes/no)");
    if (continueInvesting.toLowerCase() === "no") {
      break;
    }
  }
}
<form>
  Principal: <input type="text" id="principal"><br><br>
  Interest Rate (%): <input type="text" id="rate"><br><br>
  Years: <input type="text" id="years"><br><br>
  <button type="button" onclick="calculateInterest()">Calculate Interest</button>
</form>

This code takes inputs for the principal amount, interest rate, and number of years from an HTML form, calculates the compound interest earned after each year, and asks the user if they would like to continue investing or cash out. If the user chooses to cash out, the loop is broken and the calculation stops.

I hope it helps

CodePudding user response:

Here is a pure JavaScript solution without HTML (open your console log to view the output for each year):

function calculateInterest(principal, rate, time) {
  let amount = principal * Math.pow(1   (rate/100), time);
  let interest = amount - principal;
  return interest;
}

let principal = parseFloat(prompt("Enter the initial investment amount:"));
let rate = parseFloat(prompt("Enter the interest rate (% per year):"));
let time = 10;
let invested = true;

for (let i = 1; i <= time; i  ) {
  if (!invested) {
    break;
  }
  let interest = calculateInterest(principal, rate, i);
  console.log(`Year ${i}: $${interest   principal}`);
  if (i < time) {
    let decision = prompt("Do you want to continue staying invested? (Enter 'yes' or 'no'):");
    if (decision === "no") {
      invested = false;
      console.log("You have chosen to cash out. Your final investment amount is $"   (interest   principal));
    }
  }
}
if (invested) {
  console.log("You have stayed invested for 10 years. Your final investment amount is $"   (principal * Math.pow(1   (rate/100), time)));
}
  • Related