The calculator is spitting out some strange outcomes

My calculator is producing some strange results when I input certain values. I have added it to jsfiddle so you can take a quick look.

For example, when I enter 4000 as the principal, set the duration to 1 year, and choose a 1% interest rate, the calculator displays repetitive values for both the principal and interest. Additionally, it skips every other month. However, if I increase the interest rate to 20%, the calculation starts to display the correct results for both the principal and interest. It's quite puzzling.

I suspect the issue lies with the 'intr' variable, but I'm unsure how to resolve it.


function calculateTotal(){
    var body = document.body;
    var tbl = document.createElement('table');
    tbl.setAttribute('id', 'results');
    var tblBody = document.createElement('tbody');
    var tndiv = document.getElementById('tdcontainer');

    for (var j = 1; j < payments; j++){
        var row = document.createElement('tr');
    
        temp = round(principal);
        intr = round((monthly * payments) - principal);
        
        while(temp>0 && intr>0){    
            if(tndiv != null){
                var cell = document.createElement('td');
                var cell2 = document.createElement('td');
                var cell3 = document.createElement('td');            
    
                var ndiv =  round(temp);
                var intRate = round((monthly * payments) - principal);
                var monthlyNum = j;
    
                cell.innerHTML = ndiv;
                cell2.innerHTML = monthlyNum;
                cell3.innerHTML = intRate;
    
                row.appendChild(cell);
                row.appendChild(cell2);
                row.appendChild(cell3);
    
                j++;
            }
    
            temp-=monthly;
            intr-=monthly;
            tblBody.appendChild(row);
        }
        
        tbl.appendChild(tblBody);
        body.appendChild(tbl);
        tbl.setAttribute("border", "1");
    }
}

Answer №1

Timestamp: 07.11.2019 08:42:50
Issue: document.loandata.payment is not defined
Source File: http://examplewebsite.com/index.html
Line: 56

To fix this problem, use

document.getElementById('loandata').payment
. You can also optimize your code by utilizing
var form = document.getElementById('loandata');
and then accessing form elements like form.payment.

In other instances, make sure to access elements using their IDs with

document.getElementById('yourId')
, rather than document.yourId or document.someElement.yourId.

Additionally, ensure that you include the missing quote after the semicolon in your HTML:

<div id="visualization" style="width: 750px;"></div>

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

Properly configuring paths in react-native for smooth navigation

When working on my React-Native project, I noticed that my import paths look something like this: import { ScreenContainer, SLButton, SLTextInput, } from '../../../../../components'; import { KeyBoardTypes } from '../../../../../enums ...

Can the characteristics of a group of objects be combined and sorted based on a different attribute?

Feeling a bit puzzled by my title, Here's the issue at hand - I aim to destructure an array of objects to utilize its properties in a chartJS Line Graph. Take a look at the replicated array below: [{ "page": "Page 1", &qu ...

Tips on enlarging the header size in ion-action-sheet within the VueJS framework of Ionic

Recently I started using Vue along with the ionic framework. This is a snippet of code from my application: <ion-action-sheet :is-open="isActionSheetOpen" header="Choose Payment" mode="ios" :buttons="buttons&qu ...

Executing an external Python script within a Vue application's terminal locally

Hello, I am new to using Vue.js and Firebase. Currently, I am working on creating a user interface for a network intrusion detection system with Vue.js. I have developed a Python script that allows me to send the terminal output to Firebase. Right now, I a ...

Transfer the data stored in the ts variable to a JavaScript file

Is it possible to utilize the content of a ts variable in a js file? I find myself at a loss realizing I am unsure of how to achieve this. Please provide any simple implementation suggestions if available. In my ts file, there is a printedOption that I w ...

In Javascript, a variable is being defined by combining a string with another variable through concatenation

Hey, I'm relatively new to working with Javascript and am facing an issue that I believe can be resolved easily with the right syntax. The problem lies in setting a variable (totalRow1) where it needs to be concatenated with a string and another vari ...

Decoding arrays of JSON data

Receiving "chunked" data (json arrays) on the front-end via "xhr" (onprogress). Handling delayed chunks is easy - just remember response length and offset. The challenge comes when multiple "chunked" responses arrive simultaneously, resulting in an unpars ...

Is there a way to have the even numbers in an array printed before the odd numbers?

Looking for a way to separate even and odd numbers into two arrays? This function will do just that, but with a twist - the evens array will be printed before the odds. var numbersArray = [1,2,34,54,55,34,32,11,19,17,54,66,13]; function customDivider(n ...

Exploring the realm of arrays in jQuery and JavaScript

Seeking assistance as a beginner in Javascript/jQuery, I am looking for guidance on the following challenge: I have created a basic form with 7 questions, each containing 3 radio buttons/answers (except for question 5 which has 8 possible choices). My goa ...

What is the method for configuring input in a session without having to submit it?

I am encountering a major issue with my PHP MVC pattern website. On one page, I did not implement a "POST" submit method and now I am facing difficulties in passing quantity to the products/order page. If I use a Submit button, the next page does not funct ...

Is there a way to automate clicking a link in Internet Explorer using code?

Encountering a unique issue with IE related to this function: function downloadFileFromUserControl(filename) { var name = filename.split("/"); var fName = name[name.length - 1]; console.log("IE seems sleepy during this request"); var link ...

Unable to scroll to top using div scrollTop() function

Here's the fiddle I mentioned: http://jsfiddle.net/TLkmK/ <div class="test" style="height:100px;width:70px;overflow:auto"> Some text here that needs scrolling. </div> alert($('.test').scrollTop()); Feel free to scroll down ...

Remove all instances of an empty array within a larger array

Is there a way to eliminate an empty array from within a parent array prior to adding a new element? In the structure provided, it appears as follows: var parentArray = [ ['123', '345'], [], ['12'] ] The goal is to remove t ...

Index similar to JavaScript-Meadow

Is there a way to create a table of contents like the ones seen on sites such as JavaScript Gardens? How do they determine which section is currently active and are there any recommended JavaScript libraries that can help achieve this functionality? Edit: ...

Encountering an error message stating "Please enable JavaScript to run this application" when making React API calls after deploying a ReactJs app on the Firebase server

I'm currently working on a React JS app that calls various APIs. The backend of this application is a NodeJs server deployed in GCloud. While everything runs smoothly when I test the React app locally, I encountered an issue after deploying it to Fir ...

Height of the div dynamically increases upwards

Just a quick question - is there a way to make position: fixed divs at the bottom of an element (such as the body) grow upwards as content is dynamically added? Maybe something like anchor: bottom or increase: up? I'm thinking that using JavaScript m ...

Incorporating groovy script into HTML: Is it possible, akin to embedding JavaScript

Can groovy script be embedded in HTML similar to JavaScript? I am interested in creating an onclick button event that utilizes groovy script instead of JavaScript. Thank you. ...

Tips for stopping a click from going through a fixed element to the one in the background

My website features a fixed positioned header with a search box, overlaying content below it (thanks to the higher z-index). When I click on the search box, an event handler is triggered. However, the click response also passes through the header to the ...

JavaScript code to alter the timeout duration for image changes: Update

I am working on a fun project that involves alternating between two images, one green and the other black. Here is the code snippet: <script> var switchImage = 1; var delayTime = 500; switchImages() function switchImages() { if (switchImage == 1) ...

Issues with loading a 3D model on my hosting platform

After successfully testing my web page locally, I uploaded it to my server and encountered an issue with my 3D model not loading. What could be causing this problem? The code snippet responsible for loading the 3D model is as follows: textureLoader = new ...