2017-03-18 57 views
1

我有我喜歡這個創建的數組:類型錯誤:排列形式[i]是未定義的,但它被定義

var gelenveri = document.getElementById("cakalinput").value; 

var arrayal = []; 

arrayal = gelenveri.split(" "); 

我用這種排列形式用於我的功能,但我得到一個錯誤(類型錯誤:排列形式[I ]是不確定的,但它被定義)在這個while語句這裏面另一個用於循環:

while(((!(arrayal[i].indexOf('%') > -1)) || (arrayal[i] != "-"))){ 

temizarray[j] = temizarray[j] + arrayal[i]; 
i++; 


} 

當我嘗試看看的typeof(排列形式[1])是字符串類型。一個小時後我無法解決這個問題。任何想法?

這是我的全部代碼:

function cakallik() { 
    var gelenveri = document.getElementById("cakalinput").value; 
    var arrayal = []; 
    arrayal = gelenveri.split(" "); 
    var uzunluk = arrayal.length; 
    var temizarray = []; 
    var j = 0; 
    var i = 0; 

    for (; i < uzunluk;) { 
     temizarray[j] = arrayal[i]; //ilk satır alındı 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; //ikincisatır alındı 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; //üçüncü satır alındı 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; //dördüncü alındı 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; // beşinci alındı 
     i++; 

     while (arrayal[i] != "-") { 
      temizarray[j] = temizarray[j] + arrayal[i]; 
      i++; 
     } 

     i++; 
     j++; 
     temizarray[j] = arrayal[i]; 
     i++; 
     console.log(typeof (arrayal[i])); 

     while (((!(arrayal[i].indexOf('%') > -1)) || (arrayal[i] != "-"))) { 
      temizarray[j] = temizarray[j] + arrayal[i]; 
      i++; 
     } 

     i++; 
     j++; 
     temizarray[j] = arrayal[i]; 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; 
     i++; 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; 
     i++; 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; 
     i++; 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; 
     i++; 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; 
     i++; 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; 
     i++; 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; 
     i++; 
     j++; 
     temizarray[j] = arrayal[i]; 
    } 

    console.log(temizarray); 
} 
+0

before while while循環檢查'如果arrayal.length> 0' –

+0

@TamilSelvanC仍然會出現該錯誤。你應該停止'while' –

+0

我想'我++;'是造成這個問題試圖把它放在while循環之外...... –

回答

0
arrayal[i] is undefined 

錯誤消息是相當清楚的。雖然arrayal已被定義,但arrayal[i]未定義,因爲您試圖訪問未在該數組中顯示的元素。

Inshort,您的數組arrayal長度小於i,如果arrayal也應該添加條件以檢查長度。

while(arrayal.length < i && ((!(arrayal[i].indexOf('%') > -1)) || (arrayal[i] != "-"))){ 

temizarray[j] = temizarray[j] + arrayal[i]; 
i++; 


} 
+0

但我的for循環是(;我 capzyel

+0

我爲你加了整個代碼 – capzyel

+0

抱歉,這是我的不好。你沒事吧:) – capzyel

相關問題