JavaScript数学函数不起作用并返回Nan

时间:2018-10-12 14:06:58

标签: javascript function math nan

我创建了不起作用的数学函数。当我从另一个函数调用该函数时,它将返回NaN而不是数字。

当我声明变量时,“计算”函数中的代码块可以在函数外部正常工作,但是当我将其称为函数时,应将它们作为参数添加。

谁能告诉我为什么“计算”功能不起作用?

function gatherInfo() {
    var clientAge = document.getElementById('clientAge').value;
    var propertyValue = document.getElementById('propertyValue').value;
    var OSmortgage = document.getElementById('OSmortgage').value;
    calculate(clientAge, propertyValue, OSmortgage);
}

var subButton = document.getElementById('subButton');
subButton.addEventListener('click', gatherInfo, false);


function calculate(clientAge, propertyValue, OSmortgage) {
    var ageArray = [];
    var i;
    for (i = 55; i < 96; i++) {
        ageArray.push(i);
    }
    var percentArray = [25.6, 26.6, 27.6, 28.6, 29.6, 32.5, 33.5, 34.5, 35.5, 36.5, 37.5, 38.5, 39.5, 40.5, 41.5, 42.5, 43.2, 44.4, 45.6, 46.8, 48, 49.2, 50.4, 51, 51.5, 52, 52.5, 53, 53.5, 54, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 55];

    var ageIndex = ageArray.indexOf(clientAge);
    var percentage = percentArray[ageIndex];
    var amount = ((propertyValue - OSmortgage) / 100) * percentage;
    result(amount, clientAge);
}


function result(amount, clientAge) {

    var result = document.getElementById('result');
    if (clientAge < 55) {
        result.textContent = 'You do not qualify for equity release.';
    } else {
        result.textContent = 'You could release £' + amount + ' of your property\'s value.';
    }
}

我的HTML是:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Equity Release Calculator</title>
</head>

<body>
    <h1>Equity Release Calculator</h1>

    <form id="form" method="get">

        <p>Age
        <input type="number" id="clientAge" required="required"> 
        </p>

        <p>Property value
        <input type="number" id="propertyValue" required="required">    
        </p>       

        <p>Outstanding mortgage amount
        <input type="number" id="OSmortgage" required="required">    
        </p>

        <p>Submit
        <input type="button" id="subButton" value="Get my quote">    
        </p>       

    </form>


    <p id="result"></p>

</body>
<script src="script.js" type="text/javascript"></script>   
</html>

3 个答案:

答案 0 :(得分:0)

将代码放置在calculate函数内部后,它将更改这些变量的作用域(仅限于calculate函数本身),这意味着您无法在函数外部访问它们。

您需要在此函数中添加“ return”以访问输出。

例如

function calculate(clientAge, propertyValue, OSmortgage) {
    var ageArray = [];
    var i;
    for (i = 55; i < 96; i++) {
        ageArray.push(i);
    }
    var percentArray = [25.6, 26.6, 27.6, 28.6, 29.6, 32.5, 33.5, 34.5, 35.5, 36.5, 37.5, 38.5, 39.5, 40.5, 41.5, 42.5, 43.2, 44.4, 45.6, 46.8, 48, 49.2, 50.4, 51, 51.5, 52, 52.5, 53, 53.5, 54, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 55];

    var ageIndex = ageArray.indexOf(clientAge);
    var percentage = percentArray[ageIndex];
    var amount = ((propertyValue - OSmortgage) / 100) * percentage;

    return amount;
}

答案 1 :(得分:0)

问题正在解析,我已经更正了您的代码,请检查并告知我。希望它对您有用:)

  

var ageIndex = ageArray.indexOf(parseFloat(clientAge));       var percent = percentArray [parseFloat(ageIndex)];       var amount =((parseFloat(propertyValue)-parseFloat(OSmortgage))/ 100)* parseFloat(percentage);

function gatherInfo() {
debugger
    var clientAge = document.getElementById('clientAge').value;
    var propertyValue = document.getElementById('propertyValue').value;
    var OSmortgage = document.getElementById('OSmortgage').value;
    calculate(clientAge, propertyValue, OSmortgage);
}

var subButton = document.getElementById('subButton');
subButton.addEventListener('click', gatherInfo, false);


function calculate(clientAge, propertyValue, OSmortgage) {
    var ageArray = [];
    var i;
    for (i = 55; i < 96; i++) {
        ageArray.push(i);
    }
    var percentArray = [25.6, 26.6, 27.6, 28.6, 29.6, 32.5, 33.5, 34.5, 35.5, 36.5, 37.5, 38.5, 39.5, 40.5, 41.5, 42.5, 43.2, 44.4, 45.6, 46.8, 48, 49.2, 50.4, 51, 51.5, 52, 52.5, 53, 53.5, 54, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 55];

    var ageIndex = ageArray.indexOf(parseFloat(clientAge));
    var percentage = percentArray[parseFloat(ageIndex)];
    var amount = ((parseFloat(propertyValue) - parseFloat(OSmortgage)) / 100) * parseFloat(percentage);
    alert("amount " + amount);
}


function result(amount, clientAge) {

    var result = document.getElementById('result');
    if (clientAge < 55) {
        result.textContent = 'You do not qualify for equity release.';
    } else {
        result.textContent = 'You could release £' + amount + ' of your property\'s value.';
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Equity Release Calculator</h1>

    <form id="form" method="get">

        <p>Age
        <input type="number" id="clientAge" required="required"> 
        </p>

        <p>Property value
        <input type="number" id="propertyValue" required="required">    
        </p>       

        <p>Outstanding mortgage amount
        <input type="number" id="OSmortgage" required="required">    
        </p>

        <p>Submit
        <input type="button" id="subButton" value="Get my quote">    
        </p>       

    </form>


    <p id="result"></p>

答案 2 :(得分:0)

对所有变量使用parseint或parsefloat:

var ageIndex = ageArray.indexOf(parseFloat(clientAge)); 
var percentage = percentArray[parseFloat(ageIndex)]; 
var amount = ((parseFloat(propertyValue) - parseFloat(OSmortgage)) / 100) * parseFloat(percentage);