2015-04-02 119 views
1

第一字第一個字符第二字第二字符和上直到陣列

words = [ 
 
    "January", 
 
    "lacks", 
 
    "caveats", 
 
    "hazardous", 
 
    "DOORS", 
 
    "crying", 
 
    "arrogantly", 
 
    "climate", 
 
    "proponent", 
 
    "rebuttal" 
 
    ]; // answer Has to be JavaScript 
 
     // i can only get it right till JavaS 
 

 
function decode(words){ 
 
    
 
    for(i=0;i<words.length;i++){ 
 
     msg = words[i].charAt(i); 
 
     
 
     while(i>6 && i<=words.length){ 
 
      j=0; 
 
      msg = words[i].charAt(j); 
 
      j++; 
 
     } 
 
     console.log(msg); 
 
    } 
 
    
 
    
 
} 
 
decode(words);

J 
a 
v 
a 
S 
g 
n 

t 

的端部對於第1個字,抓住第一字符,第二個字中,第2個字符等上。當你到達第六個單詞時,再從第一個角色開始。

回答

0

%給你i的除法的用於5.
0%5 = 0,1%5 = 1,...,5%,其餘5 = 0等..

function decode(words){ 
    for(i=0;i<words.length;i++){ 
     msg = words[i].charAt(i%5); 
     console.log(msg); 
    } 
} 
+0

(i%5)是正確的感謝您的幫助 – 2015-04-02 22:47:34

+0

是的,我編輯過,如果你想要一個解釋 – 2015-04-02 22:51:26

相關問題