Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript For循环在代码中的某一行之后不起作用_Javascript_Arrays_For Loop_Split_Alert - Fatal编程技术网

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

JavaScript For循环在代码中的某一行之后不起作用,javascript,arrays,for-loop,split,alert,Javascript,Arrays,For Loop,Split,Alert,我正在用JavaScript做一个简单的练习,包括将二进制转换为其各自的十进制值。。。我觉得我快要完成这个项目了,但由于某种原因,我的for循环在某一行之后就不能工作了 //function is called after the press of a button function convertToBinary (){ oText= document.all?document.all("inout"):document.getElementById("inout"); //i

我正在用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确实存储了值,检查了一千次,但在这一行之后,一切都停止了,我似乎找不到原因。下面是从我的文本编辑器复制/粘贴的代码的其余部分

    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代表int

没有明显的语法错误。如果不是真的有必要的话,我不会发布这个练习的解决方案,我会很感激。我可能还没有做好,但我喜欢自己仔细思考算法:p。我的问题是,循环好像没有满足条件一样,只是被跳过了。我使用Opera Next作为web浏览器,并且inspect元素控制台也不会显示任何错误

提前谢谢你。

这应该是

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

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

你错卖了这个,长度

你的长度有一个输入错误。应该是长度。这是真代码还是复制错误?等一下,你说的是二进制到它各自的十进制值。但是你的函数名是converttobinary?你要往哪边走?如果你想得到一个小数点,你可以用基数2调用parseInt,除非这是为了学习如何手动操作。@Barmar G我累了。。。那是一个拼写错误。。。我现在就去睡觉。你没有纠正第一个拼写错误:错过了那个,对不起。
for(var x = aBina.lenght-1; x >= 0; x--){
 for(y = 0; y < aText.lenght;y++)