簡體   English   中英

是不是i == [i-1]與i == [i + 1]相同,假設循環從i = 1開始?

[英]Isn't i == [i-1] the same thing as i == [i+1] assuming that loop begins at i = 1?

我無法理解為什么下面的兩個腳本產生不同的結果。 有人可以幫助我理解為什么這兩個腳本的方式有所不同?

 var str = "this needs to be capped!", str1 = str.split(""), strarray = []; for(var i = 1; i < str.length; i++) { if(str[i] == " ") { strarray.push(str[i]); strarray.push(str[i+1].toUpperCase()); } else if(strarray[i] == strarray[i-1]) { strarray.push(str[i]); } } console.log(strarray.join(',')) 

輸出:h,i,s,N,e,e,d,s,T,o,B,e,C,a,p,p,e,d ,!

VS

 var str = "this needs to be capped!", str1 = str.split(""), strarray = []; for(var i = 1; i < str.length; i++) { if(str[i] == " ") { strarray.push(str[i]); strarray.push(str[i+1].toUpperCase()); } else if(strarray[i] == strarray[i+1]) { strarray.push(str[i]); } } console.log(strarray.join(',')) 

輸出:h,i,s,N,n,e,e,d,s,T,o,B,e,C,a,p,p,e,d ,!

簡而言之,這里的區別是小寫'n'。

發生這種情況是因為您將undefinedundefined進行比較。

對於第一種情況,這是發生的事情

    i       str[i]         strarray
   _________________________________
    1       h               [h]
    2       i               [h,i]
    3       s               [h,i,s]
    4       ' '             [h,i,s,,N]
    5       n               [h,i,s,,N]   //here it is comparing undefined=='N' 
                                         //which is false no push in array
    .
    .

對於第二種情況,這就是發生的事情

    i       str[i]         strarray
   _________________________________
    1       h               [h]
    2       i               [h,i]
    3       s               [h,i,s]
    4       ' '             [h,i,s,,N]
    5       n               [h,i,s,,N,n]   //here it is comparing undefined==undefined 
                                         //which is true so 'n' was push in array
    .
    .
         /*now whenever space encounters it will push 2 element to
        strarray(space and next character), and for the next i strarray[i] 
        will not be undefined while strarray[i+1] will be undefined so for 
        that i no push into array. */

您正在比較字符串(不可變)中的位置與您在循環中不斷修改的數組中的位置。

在你的第二個循環中,因為你總是在“前方”( strarray[i + 1] ),你總是將undefinedundefined進行比較,它總是相等的。

在數組中, push()元素使元素為零索引(而你的i計數器從1開始)。

這就是為什么,在你添加第一個大寫字母之前,索引是后面的一個,但不是在那之后(你為每個空格字符向數組添加2個元素)。

這解釋了為什么你得到一個額外的“n”(第一個空格后的第一個字母),以及為什么在第一個實例之后沒有復制該行為的原因:

(我省略了第一個循環,因為它不起眼)

 var str = "this needs to be capped!"; var strarray = []; for (var i = 1; i < str.length; i++) { if (str[i] == " ") { console.log('space', str[i + 1].toUpperCase()); strarray.push(str[i]); strarray.push(str[i + 1].toUpperCase()); } else { console.log('i: ', i, ' / str[i]: ', str[i], ' / strarray[i]: ', strarray[i], ' / strarray[i + 1]: ', strarray[i + 1], ' / str.length: ', str.length, ' / strarray.length: ', strarray.length); if (i > strarray.length) { console.log('behind'); } else { console.log('caught up'); } console.log(); if (strarray[i] == strarray[i + 1]) { strarray.push(str[i]); } } } console.log(strarray.join(',')); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM