Can you demonstrate how to repeatedly increase a running total using a for loop until the loop ends?

Looking at the for loop. The goal is to have the interest calculated based on the latest value variable. Currently, the script generates the same interest rate and value for each year. What I desire is for it to use the previous value variable to determine the new value for each year. Here is the output in a browser: Year: 1 Interest: 750 Value: 10750

Year: 2 Interest: 750 Value: 10750

Year: 3 Interest: 750 Value: 10750

Year: 4 Interest: 750 Value: 10750

Year: 5 Interest: 750 Value: 10750

Initial investment amount = 10000 Interest rate = 7.5 Years = 5 Future Value is 10750

Thank you for using the Future Value application. Both the value and the interest should be incrementing.


    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">  
        <title>Future Value Application</title>
<script>
    var futureValue;


    var investment = prompt("Enter investment amount as xxxxx.xx", 10000);
    investment = parseFloat(investment);
    var rate = prompt("'Enter interest rate as xx.x", 7.5);
    rate = parseFloat(rate);
    var years = prompt("Enter number of years", 10);
    years = parseInt(years);



    // calculate future value
    futureValue = investment;


    for (var i = 1; i <= years; i++ ) {
        var cInterest;
        var value;
        document.write("Year: " +(i) + "<br>");
        cInterest = futureValue * rate / 100;
        cInterest = parseInt(cInterest);
        document.write("Interest: " + cInterest + "<br>");
        value = futureValue + cInterest;
        value = parseInt(value);
        document.write ("Value: " + value + "<br><br>");
        cInterest += value;
    }
    futureValue = parseInt(futureValue);


 </script>
   </head>
   <body>
<main>
    <script>
        document.write("Investment amount = " + investment);
        document.write(" Interest rate = " + rate);
        document.write(" Years = " + years);
        document.write(" Future Value is " + (futureValue + futureValue* rate / 100) + "<br><br>");
    </script>
    Thanks for using the Future Value application.
</main>

Answer №1

Have you considered checking out this helpful resource: https://msdn.microsoft.com/library/bzt2dkta(v=vs.94).aspx

In response to Alexei's comment, it is important to note that both the value and cInterest variables are declared within the for loop in your code. This means that their values are reset after each iteration since they only exist within the scope of the loop. To address this issue, you can try:

    var cInterest;
    var value; // It's unclear what you intend to do with this variable.
    for (var i = 1; i <= years; i++ ) {

    document.write("Year: " +(i) + "<br>");
    cInterest = futureValue * rate / 100;
    cInterest = parseInt(cInterest);
    document.write("Interest: " + cInterest + "<br>");
    value = futureValue + cInterest; // The 'value' variable gets 'reset' here.
    value = parseInt(value);
    document.write ("Value: " + value + "<br><br>");
    cInterest += value;
}
futureValue = parseInt(futureValue);

By following this approach, your cInterest calculation should be correct. However, if you have specific intentions for the value variable, further clarification may be needed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Importance of route prioritization in Express.js

I am looking to establish a specific order of priority for routes in express as follows: custom URLs, static files, error pages. This is how I currently have it set up: let router = express.Router(); // Custom URLs router.get("/foo", ...); app.use(rout ...

Utilize Vue.js and express.js to distribute HTML files securely

Currently, my tech stack includes Vue.js for the frontend and Express.js for the backend. When I kick off the express.js server using npm start, my goal is to serve the Vue frontend component. By utilizing the Vue generator and Express generator, I attemp ...

Unable to locate a type definition file for module 'vue-xxx'

I keep encountering an error whenever I attempt to add a 3rd party Vue.js library to my project: Could not find a declaration file for module 'vue-xxx' Libraries like 'vue-treeselect', 'vue-select', and 'vue-multiselect ...

What is the best way to incorporate my CSS file into an HTML file when using Express?

When I try to host my html file using Express, it seems that my CSS is not getting applied. Why is this happening and what is the best way to include my CSS file with Express? const express = require('express'); const bodyParser = require('b ...

Transferring JSON data using $.post in Express/NodeJS

Below is the contents of my app.js file: var express = require('express'); var app = express(); var path = require('path'); var $ = require('jquery'); var nodemailer = require('nodemailer'); app.use('/static&a ...

The issue that arises is that only the initial button is able to successfully trigger the opening of

My goal is to create a forum where users can reply to posts by clicking on a "Reply" button that triggers a Bootstrap modal. However, I am facing an issue where the modal only opens when clicking on the first button in a row due to it being placed within a ...

Is there a way to deactivate middleware in Node, Express, and Mocha?

My application features a hello world app structured as follows: let clientAuthMiddleware = () => (req, res, next) => { if (!req.client.authorized) { return res.status(401).send('Invalid client certificate authentication.'); } ret ...

Modifying the data of an HTML form using Javascript, encrypting the information, and transferring it to PHP

I am new to PHP and have a simple code management question. I would like to create an input box in HTML where someone can enter their name, and with the use of Javascript, generate a link with an encoded version of the name preceded by a website string. Wh ...

Sending a result back to a Silverlight user control from a slightly intricate JavaScript function

I am working on a Silverlight user control that contains a textbox and a button. Within this Silverlight page, there can be multiple instances of these user controls. My goal is to have a JavaScript function trigger when the button is clicked. This functi ...

When making a POST request with axios, the req.body object appears to be empty. However, when using the 'request' module, the body is populated as expected

Providing some context - My NodeJS server is running on port 3001 and my React application on port 3000. I've configured a proxy in my React application's package.json to redirect all requests to port 3001 - "proxy": "http://localhost:3001" H ...

Tips for extracting variables from a querystring in Express?

I am trying to retrieve values sent to the server: "/stuff?a=a&b=b&c=c" Can you please advise me on how to extract these values using express? So far, I have attempted... app.get( "/stuff?:a&:b&:c", function( req, res ){}); ...but unfo ...

PHP form submission upon conditions being satisfied

I am not utilizing Javascript as it is disabled in my current environment. Here is the code that I would like you to review. I am attempting to use PHP to replicate the functionality of Javascript or jQuery's simple : document.form2.submit() <div ...

By default, make the initial element of the list the selected option in an AngularJS select input

I'm running into an issue with setting the first element in my ng-repeat list within a select input. Here is the code snippet causing the problem: <div> <span >OF</span> <select ng-model="eclatementCourante.ordreFabricationId" n ...

Is the concept of client fanout truly beneficial?

I'm in the process of constructing a community platform on firebase that features timelines and posts similar to those found on Facebook. When a user creates a post, it should automatically appear on the timelines of their followers. Google proposes ...

Creating a list that renders responsively using ReactJS

I'm currently working on implementing a responsive search bar that filters through a list of pure components (up to 100 components displayed at once). However, I've noticed there is a slight half-second delay between typing the first letter and s ...

Encountering difficulties in compiling Dynamic HTML with the $compile function

I'm attempting to incorporate dynamic HTML into my code with the following lines: var el = $compile('<a ng-controller=\"tableController\" ng-click=\"open\">...ReadMore</a>')($scope); But I'm encounterin ...

Developing desktop applications with Angular 2 using NWjs

Looking to create an Angular 2 Desktop App using NWjs. Where can I find the entry point? Could someone provide some examples of developing Angular 2 Desktop Apps with NW.js? ...

Error in TypeScript: The property 'data' is not found within type '{ children?: ReactNode; }'. (ts2339)

Question I am currently working on a project using BlitzJS. While fetching some data, I encountered a Typescript issue that says: Property 'data' does not exist on type '{ children?: ReactNode; }'.ts(2339) import { BlitzPage } from &q ...

JavaScript form validation issue unresolved

When I attempt to validate form fields using Javascript functions, they seem to not load or check the field upon clicking the submit button. <html> <body> <?php require_once('logic.php'); ?> <h1>New Region/Entit ...

The class name is not defined for a certain child element in the icon creation function

Currently, I am developing a Vue2 web application using Leaflet and marker-cluster. I am encountering an issue with the iconCreateFunction option in my template: <v-marker-cluster :options="{ iconCreateFunction: iconCreateClsPrg}"> ...