简体   繁体   English

JavaScript For循环在代码中的特定行之后不起作用

[英]JavaScript For loop not working after a certain line in the code

I'm doing a simple exercise meant to be in JavaScript that consists of turning a binary into its respective decimal value... I feel I'm close to finishing the project but for some reason my for loops wont work after a certain line. 我正在用JavaScript进行一次简单的练习,其中包括将二进制转换为相应的十进制值...我觉得我即将完成项目,但由于某种原因,我的for循环在某一行之后将无法工作。

//function is called after the press of a button
function convertToBinary (){
    oText= document.all?document.all("inout"):document.getElementById("inout");
    //inout represents a textarea
    sText= oText.value;
    alert(sText);
    aText = sText.split(/\n\r|\n|\r/g); //got this line after searching this site for solutions

    /*alert(aText[1]);
    alert(aText[2]);
    alert(aText[3]);*/

aText does have values stored, checked a thousand times, but after this line everything goes down, and i can't seem to find the reason why. aText确实存储了值,检查了一千次,但是在这行之后一切都掉了,我似乎找不到原因。 Here is the rest of the code copy/pasted from my text editor. 这是从我的文本编辑器复制/粘贴的其余代码。

    for(y = 0; y < aText.lenght;y++){
        alert(aText[y]);
    }

    iCurrent=0;
    aBina= aText[0].split("");
    for(var x = aBina.lenght-1; x >= 0; x--){
        iCurrent = (iCurrent*2) + parseInt(aBina[x]);
        alert(iCurrent);
    }
    aAsci[0]=iCurrent;

    alert(iCurrent); //Alerts 0
 }

NOTE: variables and arrays are properly defined in the first few lines of the code, i have used them in a previous function that does the opposite(convert decimals to binary) and that works fine. 注意:变量和数组在代码的前几行中已正确定义,我在以前的函数中使用了它们,该函数执行相反的操作(将小数转换为二进制)并且工作正常。 I was sure to clean each array and variable after the function is done. 我确定函数完成后要清理每个数组和变量。 ("o" is for objects, "s" for strings, "a" for arrays, "i" for int's). (“ o”代表对象,“ s”代表字符串,“ a”代表数组,“ i”代表整数)。

There is no apparent syntax error to be seen. 没有明显的语法错误可以看到。 I would appreciate not posting the solution for the exercise unless it is really necessary, i might not be doing it right just yet but I like to think through the algorithms my self :p. 除非确实有必要,否则我不希望为该练习发布解决方案,我可能还没有做好,但是我想通过算法思考一下我自己:p。 My problem is for loops are as if the condition was not met, they are just being skipped. 我的问题是循环就像没有满足条件一样,只是被跳过了。 I'm using Opera Next as my web browser and the "inspect element" Console does not show any errors either. 我将Opera Next用作Web浏览器,并且“检查元素”控制台也未显示任何错误。

Thank you in advance. 先感谢您。

This should be, 应该是

for(y = 0; y < aText.length;y++) 

and this 和这个

for(var x = aBina.length-1; x >= 0; x--){

instead 代替

for(var x = aBina.lenght-1; x >= 0; x--){
 for(y = 0; y < aText.lenght;y++) 

you mispelled this one, "LENGTH" 您拼错了这个“ LENGTH”

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

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