JavaScript For循环在代码的某一行之后不起作用

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

本文关键字:一行 不起作用 之后 循环 For 代码 JavaScript      更新时间:2023-09-26

我正在做一个简单的练习,意思是在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]);*/

text确实存储了值,检查了一千次,但在这一行之后,一切都下降了,我似乎找不到原因。以下是从我的文本编辑器复制/粘贴的其余代码。

    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
 }

注意:变量和数组是在代码的前几行中正确定义的,我在前面的函数中使用了它们,它的作用正好相反(将小数转换为二进制),并且工作得很好。我确信在函数完成后清理每个数组和变量。("o"代表对象,"s"代表字符串,"a"代表数组,"i"代表整型)。

没有明显的语法错误。除非真的有必要,否则我不会发布练习的解决方案,我可能还没有做对,但我喜欢自己思考算法:p。我的问题是for循环就好像条件没有满足,它们只是被跳过。我使用Opera Next作为我的web浏览器,"inspect element"控制台也没有显示任何错误。

提前感谢。

应该是,

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

和this

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

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

这个你拼错了,"LENGTH"