第1个单词第1个字符第2个单词第2个字符,直到数组结束

时间:2015-04-02 22:42:45

标签: javascript



 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);
&#13;
&#13;
&#13;

J
a
v
a
S
g
n

t

对于第一个单词,抓住第一个字符,第二个字,第二个字符,依此类推。当你到达第6个单词时,再次从第1个单词开始。

1 个答案:

答案 0 :(得分: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);
    }
}