简体   繁体   English

Javascript 中的 Nan 错误 - 我做错了什么?

[英]Nan error in Javascript - what am I doing wrong?

I'm attempted to learn Javascript (again) due to a new role at work and am struggling a little.由于工作中的新角色,我试图(再次)学习 Javascript 并且有点挣扎。 As a learning exercise I'm trying to code a simple set of functions to get used to the simple skills and have come across a problem.作为一项学习练习,我正在尝试编写一组简单的函数来习惯简单的技能并遇到问题。 Code is below, but I have a webpage I've built to capture some data and the pass into some simple interest calculations.代码如下,但我已经建立了一个网页来捕获一些数据并传递给一些简单的兴趣计算。 Unfortunately I'm getting a Nan error when the values hit the functions and I don't know how to resolve it.不幸的是,当值命中函数并且我不知道如何解决它时,我得到了一个 Nan 错误。

I've tried +document, parse(float) and capturing the innerhtml from the fields, but none of those worked so I'm a little stumped.我已经尝试过 +document、parse(float) 和从字段中捕获 innerhtml,但这些都不起作用,所以我有点难过。 I'm certain it's a typecasting issue but no idea how to resolve it.我确定这是一个类型转换问题,但不知道如何解决它。

I'm capturing the fields used in the calculations via:我正在通过以下方式捕获计算中使用的字段:

 var runCalculations = new function() { this.calculateTotalPayable = function() { this.submitInitialValues(); this.simpleInterestCalc(); this.monthlyCompInterestCalc(); this.dailyCompInterestCalc(); } this.submitInitialValues = function() { //Initial loan or investment amount var initialAmount = +document.getElementById("initialAmount").value; console.log("Initial loan amount: " + initialAmount); //Interest rate var interestRate = +document.getElementById("interestRate").value; console.log("Interest rate: " + interestRate); //Tenure of loan var loanTenure = +document.getElementById("loanTenure").value; console.log("Loan tenure: " + loanTenure); } this.simpleInterestCalc = function() { var simpleInterestTotal = 0; console.log(initialAmount + interestRate + loanTenure); simpleInterestTotal = initialAmount + (initialAmount * (interestRate / 100) * loanTenure); console.log("simple " + simpleInterestTotal); } this.monthlyCompInterestCalc = function() { const monthCompInterestTotal = 0; console.log("month " + monthCompInterestTotal); } this.dailyCompInterestCalc = function() { const dailyCompInterestTotal = 0; console.log("daily " + dailyCompInterestTotal); } }
 <form id="initialValues"> <label for="initialAmount" style="margin-left: 50px"><b>Initial loan or investment amount</b></label><br> <input type="number" id="initialAmount" name="initialAmount" style="margin-left: 50px" value="0"><br> <label for="interestRate" style="margin-left: 50px"><b>Interest rate</b></label><br> <input type="number" id="interestRate" name="interestRate" style="margin-left: 50px" value="0"><br> <label for="loanTenure" style="margin-left: 50px"><b>Loan tenure</b></label><br> <input type="number" id="loanTenure" name="loanTenure" style="margin-left: 50px" value="0"><br> <br> <input type="button" style="margin-left: 50px" value="Submit" onclick="runCalculations.calculateTotalPayable()"> <input type="reset" style="margin-left: 50px" value="Reset Values"> </form>

Your variables are defined in submitInitialValues , not in simpleInterestCalc .您的变量在submitInitialValues中定义,而不是在simpleInterestCalc中。 It will work if you move the variable declarations outside of the function.如果您将变量声明移到 function 之外,它将起作用。 Please learn about scope in JavaScript before you continue.在继续之前,请先了解JavaScript 中的 scope

The reason why it didn't simply crash with errors like 'initialAmount' is not defined is that for legacy reasons, HTML elements with IDs are exposed as global variables by default unless you define your own with the same name, so inside of simpleInterestCalc your variables were actually referencing the corresponding HTML elements themselves and not their values.它之所以没有简单地'initialAmount' is not defined类的错误而崩溃的原因是,由于遗留原因,默认情况下,具有 ID 的 HTML 元素将作为全局变量公开,除非您使用相同的名称定义自己的变量,因此在simpleInterestCalc您变量实际上是在引用相应的 HTML 元素本身,而不是它们的值。

Thank you to people for the pointers.感谢人们的指点。 I've managed to get something more usable.我设法得到了更有用的东西。 Any constructive feedback is welcome (mainly because I know any proper coder will look at what I've done and probably wince!)欢迎任何建设性的反馈(主要是因为我知道任何合适的编码人员都会看到我所做的事情并且可能会畏缩!)

const runCalculations = new function(){

this.calculateFinalValues = function(){

    //Initial loan or investment amount
    const calcInitialAmount = +document.getElementById("initialAmount").value;
    //Interest rate
    const calcInterestRate = +document.getElementById("interestRate").value;
    //Tenure of loan
    const calcLoanTenure = +document.getElementById("loanTenure").value;

    calcSimple(calcInitialAmount,calcInterestRate,calcLoanTenure);
    calcAnnualCompound(calcInitialAmount,calcInterestRate,calcLoanTenure);
    calcMonthlyCompound(calcInitialAmount,calcInterestRate,calcLoanTenure);
    calcDailyCompound(calcInitialAmount,calcInterestRate,calcLoanTenure);

    function calcSimple(calcInitialAmount,calcInterestRate,calcLoanTenure){
        let simpleFinalTotal = 0;
        simpleFinalTotal = calcInitialAmount + ((calcInitialAmount * (calcInterestRate/100) * calcLoanTenure));
        document.getElementById('simpleResult').innerHTML = simpleFinalTotal.toFixed(2);
        return document.getElementById('simpleResult').innerHTML;
    }

    function calcAnnualCompound(calcInitialAmount,calcInterestRate,calcLoanTenure){
        let annualFinalTotal = 0;
        annualFinalTotal = calcInitialAmount + calcCompoundInterest(calcInitialAmount,calcInterestRate,calcLoanTenure,1);
        document.getElementById('finalAnnualResult').innerHTML = annualFinalTotal.toFixed(2);
        return document.getElementById('finalAnnualResult').innerHTML;
    }

    function calcMonthlyCompound(calcInitialAmount,calcInterestRate,calcLoanTenure){
        let monthFinalTotal = 0;
        monthFinalTotal = calcInitialAmount + calcCompoundInterest(calcInitialAmount,calcInterestRate,calcLoanTenure,12);
        document.getElementById('finalMonthlyResult').innerHTML = monthFinalTotal.toFixed(2);
        return document.getElementById('finalMonthlyResult').innerHTML;            
    }

    function calcDailyCompound(calcInitialAmount,calcInterestRate,calcLoanTenure){
        let dailyFinalTotal = 0;
        dailyFinalTotal = calcInitialAmount + calcCompoundInterest(calcInitialAmount,calcInterestRate,calcLoanTenure,365);
        document.getElementById('finalDailyResult').innerHTML = dailyFinalTotal.toFixed(2);
        return document.getElementById('finalDailyResult').innerHTML;             
    }

    function calcCompoundInterest(calcInitialAmount,calcInterestRate,calcLoanTenure,compPerYear){
        compInterestTotal = calcInitialAmount*(((1+((calcInterestRate/100)/compPerYear))**(calcLoanTenure*compPerYear))-1);
        return compInterestTotal;
    }
}

} }

Variable capture and return to the screen is below:变量捕捉和返回画面如下:

 <form id="initialValues"> <label for="initialAmount" style="margin-left: 50px"><b>Initial loan or investment amount</b></label><br> <input type="number" id="initialAmount" name="initialAmount" style="margin-left: 50px" value="0"><br> <label for="interestRate" style="margin-left: 50px"><b>Interest rate</b></label><br> <input type="number" id="interestRate" name="interestRate" style="margin-left: 50px" value="0"><br> <label for="loanTenure" style="margin-left: 50px"><b>Loan tenure</b></label><br> <input type="number" id="loanTenure" name="loanTenure" style="margin-left: 50px" value="0"><br> <br> <input type="button" style="margin-left: 50px" value="Submit" onclick="runCalculations.calculateFinalValues()"> <input type="reset" style="margin-left: 50px" value="Reset Values"> </form> <h2>Simple Interest Calculation</h2> <p>Final amount based on simple interest where interest is added in a single amount:</p> <p type="number" id="simpleResult" style="margin-left: 50px; font-weight: bold"></p> <h2>Compound Interest</h2> <p>Final amount where interest is compounded annually:</p> <p id="finalAnnualResult" style="margin-left: 50px; font-weight: bold"><b></b></p> <p>Final amount where interest is compounded monthly:</p> <p id="finalMonthlyResult" style="margin-left: 50px; font-weight: bold"><b></b></p> <p>Final amount where interest is compounded daily:</p> <p id="finalDailyResult" style="margin-left: 50px; font-weight: bold"><b></b></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM