JavaScript循环变量返回NaN时

时间:2016-11-26 14:49:16

标签: javascript loops variables while-loop nan

我正在使用JavaScript和HTML5制作Numerology转换网络应用程序。我遇到了一个变量和循环的奇怪问题。以下是我目前形式的整个项目。

<limits>

所以基本上你把你的生日和全名输入表格。然后,脚本将名称转换为数字A = 1,B = 2 ....并将两者都减少为单个数字。这些数字将逐一添加。例。 13 = 4,55 = 10,63 = 9。

以下是用于进行缩减的一般代码。我们让PODN = 55

> clang++ --version
Apple LLVM version 8.0.0 (clang-800.0.42.1)

$ g++ --version
g++ (Debian 4.9.2-10) 4.9.2

目前,它只循环一次,但我希望将其减少到可能的最低数量。在这种情况下为1。我在想,如果我将if改变一段时间它会完成这个但是它不会像我想象的那样工作。我试图做一些改变并陷入困境。所以我开始排除故障。我添加了另一个变量来防止整个循环无限循环。

<html>
<body>

<h1>My First Numerology Project</h1>
<form>
    Month:<br>
    <input type="text" maxlength="2" name="userInputMonth" id='userInputMonth'>
    <br>
    Day:<br>
    <input type="text" maxlength="2" name="userInputDay" id='userInputDay'>
    <br>
    Year:<br>
    <input type="text" maxlength="4" name="userInputYear" id='userInputYear'>
    <br><br>
    First Name:<br>
    <input type="text" name="userInputFirstName" id='userInputFirstName'>
    <br>
    Middle Name:<br>
    <input type="text" name="userInputMiddleName" id='userInputMiddleName'>
    <br>
    Last Name:<br>
    <input type="text" name="userInputLastName" id='userInputLastName'>
    <br><br>
    <input type="button" value="Calculate" name="Calculate" onclick="BirthDayCalculation()"/>
</form>
<p id="LLNResult"></p>
<br>
<p id="OPNResult"></p>
<br>
<p id="SNResult"></p>
<br>
<p id="PODNResult"></p>
<script type="text/javascript">
//Defined var 
var LLNTemp1;
var LLNTemp2;
var LLNReduce1;
var LLNReduce2;
var NameTemp1;
var NameTemp2;
var OPNReduce1;
var OPNReduce2;
var SNReduce1;
var SNReduce2;
var PODNReduce1;
var PODNReduce2;
function BirthDayCalculation() {
    var Reduction1 = 0;
    var LLNReduceFinal2 = 0;
    var Day = document.getElementById('userInputDay').value;
    var Month = document.getElementById('userInputMonth').value;
    var Year = document.getElementById('userInputYear').value;
    var FirstName = document.getElementById('userInputFirstName').value;
    var MiddleName = document.getElementById('userInputMiddleName').value;
    var LastName = document.getElementById('userInputLastName').value;
    var LLN = ("" + Day + Month + Year).toString();
    var FullName = ("" + FirstName + MiddleName + LastName).toString().toUpperCase();
    var FullNameLength = parseInt(FullName.length);
    var LLNLength = parseInt(("" + Day + Month + Year).length);
    var i = 0;
    var LLNTemp2 = 0;
    while (i < LLNLength) {
        LLNTemp1 = parseInt(LLN.charAt(i));
        LLNTemp2 += LLNTemp1;
        i++;
    }
    if (LLNTemp2 > 9) {
        var i = 0;
        var a = parseInt(LLNTemp2.toString().length);
        var LLNReduceFinal1 = 0;
        var LLNTemp3String = LLNTemp2.toString();
            while (i < a) {
            LLNReduce1 = parseInt(LLNTemp3String.charAt(i));
            LLNReduceFinal1 += LLNReduce1;
            i++;
            }
        var LLNReduceFinal2 = 0;
        var Reduction1 = LLNReduceFinal1;
        if (LLNReduceFinal1 > 9) {
            var i = 0;
            var a = parseInt(Reduction1.toString().length);
            var LLNTemp3String = Reduction1.toString();
            while (i < a) {
                LLNReduce1 = parseInt(LLNTemp3String.charAt(i));
                LLNReduceFinal2 += LLNReduce1;
                i++;
            }
        }
    }
    var OPN = 0;
    var SN = 0;
    var PODN = 0;
    var PODNWhole = 0;
    var i = 0;
    while (i < FullNameLength) {
        NameTemp1 = FullName.charAt(i);
        var OPNTemp = 0;
        var SNTemp = 0;
        if (NameTemp1 == "A") SNTemp = 1;
        if (NameTemp1 == "J" || NameTemp1 == "S") OPNTemp = 1;
        if (NameTemp1 == "B" || NameTemp1 == "K" || NameTemp1 == "T") OPNTemp = 2;
        if (NameTemp1 == "C" || NameTemp1 == "L") OPNTemp = 3;
        if (NameTemp1 == "U") SNTemp = 3;
        if (NameTemp1 == "D" || NameTemp1 == "M" || NameTemp1 == "V") OPNTemp = 4;
        if (NameTemp1 == "E") SNTemp = 5;
        if (NameTemp1 == "N" || NameTemp1 == "W") OPNTemp = 5;
        if (NameTemp1 == "F" || NameTemp1 == "X") OPNTemp = 6;
        if (NameTemp1 == "O") SNTemp = 6;
        if (NameTemp1 == "G" || NameTemp1 == "P" || NameTemp1 == "Y") OPNTemp = 7;
        if (NameTemp1 == "H" || NameTemp1 == "Q" || NameTemp1 == "Z") OPNTemp = 8;
        if (NameTemp1 == "I") SNTemp = 9;
        if (NameTemp1 == "R") OPNTemp =9;
        OPN += OPNTemp;
        SN += SNTemp;
        PODNWhole += SNTemp;
        PODNWhole += OPNTemp;
        i++;
    }
    var PODN = PODNWhole
    var OPNReduceFinal = 0;
    if (OPN > 9) {
        var OPNLength = parseInt(OPN.toString().length);        
        var OPNTempString = OPN.toString();
        var i = 0;
        while (i < OPNLength) {
            OPNReduce1 = parseInt(OPNTempString.charAt(i));
            OPNReduceFinal += OPNReduce1;
            i++;
        }
    }
    var SNReduceFinal = 0;
    if (SN > 9) {
        var SNLength = parseInt(SN.toString().length);  
        var SNTempString = SN.toString();
        var i = 0;
        while (i < SNLength) {
            SNReduce1 = parseInt(SNTempString.charAt(i));
            SNReduceFinal += SNReduce1;
            i++;
        }
    }   
    var PODNReduceFinal = 0;
    if (PODN > 9) {
        var PODNLength = parseInt(PODN.toString().length);  
        var PODNTempString = PODN.toString();
        var i = 0;
        while (i < PODNLength) {
            PODNReduce1 = parseInt(PODNTempString.charAt(i));
            PODNReduceFinal += PODNReduce1;
            i++;
        }
//      var PODNReduceFinal = PODNReduce2;
//      var PODN = PODNReduce2;
    }
    var LLNResult = "Your Life Lesson Number Is:" + LLNTemp2 + "/" + Reduction1;
    if (LLNReduceFinal2 != 0) {
        LLNResult = LLNResult + "/" + LLNReduceFinal2;
    }
    var OPNResult = "Your Outer Personality Number is:" + OPN;
    if (OPNReduceFinal != 0) {
        var OPNResult = OPNResult + "/" + OPNReduceFinal;
    }
    var SNResult = "Your Soul Number is:" + SN;
    if (SNReduceFinal != 0) {
        var SNResult = SNResult + "/" + SNReduceFinal;
    }
    var PODNResult = "Your Path of Destiny Number is:" + PODNWhole;
    if (PODNReduceFinal != 0) {
        var PODNResult = PODNResult + "/" + PODNReduceFinal;
    }
    if (LLNTemp2 != 0) {
        document.getElementById("LLNResult").innerHTML = LLNResult;
    }
    if (PODNWhole != 0) {
        document.getElementById("OPNResult").innerHTML = OPNResult;
        document.getElementById("SNResult").innerHTML = SNResult;
        document.getElementById("PODNResult").innerHTML = PODNResult;
    }

}
</script>

</body>
</html>

使用这个我认为当PODN首先被while循环检查时,它会完成第一步。获取数字的长度,在这种情况下2.将其转换为字符串,以便它可以逐个拉出数字。按循环长度顺序拉数字并将其转换为数字。在PODNReduce2,55 = 10中将它们一起添加。设置PODNReduceFinal = 10,设置PODN = 10.然后返回第一个条件并检查PODN现在是10并且大于9.再次循环以进一步减小。这样做直到它存储在PODNReduceFinal中的单个数字。但事实并非如此。当我尝试显示PODNReduceFinal时,我得到了NaN。即使我将第一个“while”恢复为“if”,它仍然无效。我不知所措。请帮忙。

0 个答案:

没有答案