2017-04-23 30 views
0
var input = prompt(); 
var binput = input.split(""); 
var inputl = input.split(" ").length 

if (inputl % 5 == 0) { 
    binput.forEach(function(v, i) { 
     if (v === "a") { 
      binput[i] = "$" 
     } 
    }) 
} 

這裏我的問題的一個因素是,我只想要5日10日15日...的inputl元素被改變,現在它改變了整個文本if (inputl % 5 == 0)。不過,我只想抓住inputl陣列的第五個元素。抓一個陣列

回答

1

這裏我的問題是,我想只有5 10 15

試試這個

binput.forEach(function(v, i) { //iterate once 
    if((i + 1) % 5 == 0) //check if the element is 5th, 10th, 15th etc 
    { 
     binput[i] = "$"; //assign the value as you like 
    } 
}}; 

編輯:

如果你還需要檢查是否值在分配新值之前是'a',然後嘗試

binput.forEach(function(v, i) { //iterate once 
    if(v == "a" && ((i + 1) % 5 == 0)) //check if the element is 5th, 10th, 15th etc 
    { 
     binput[i] = "$"; //assign the value as you like 
    } 
}}; 
+0

應該工作,感謝你的頭腦 –

+0

我想你搞砸了的東西我不能得到它的工作,這個想法真的很好壽 –

0
if (inputl % 5 == 0){  
    binput.forEach(function(v, i) { 
    if((i + 1) % 5 == 0) {  
     if(v === "a") {  
    binput[i] = "$"  
}}  
})}  

我得到它與該工作感謝您的時間