[英]looping infinitely through multiple arrays at the same time until a condition is met
[英]Is it possible to tell a while loop to 'keep looping until this condition is not met, then execute one more time'?
這是我的循環。
if (strSplit[i] == "(") {
while (strSplit[i] != ")") {
newElement = newElement.concat(strSplit[i]);
i++;
}
/*add the closing bracket*/
newElement = newElement.concat(strSplit[i]);
i++;
}
因此,我們要做的是將數組中的元素連接在一起,使其既包含左括號又包含左括號。
我覺得有點笨拙,我不得不再重復一次連接以添加結束括號。
是否可以讓while循環“繼續循環直到不滿足此條件,然后再執行一次”?
do{
newElement = newElement.concat(strSplit[i]); i++;
}while(strSplit[i] != ")")
您可以只保存“(”和“)”的索引(我們叫索引a
和b
),然后將子字符串從a
改為b+1
如果您喜歡冒險,可以對“正則表達式”進行一些研究,找到一種更優雅的方法:)
或者,為了“強力地”解決問題,請稍等一會兒,然后再添加一行代碼,就像循環中的內容一樣。
var count = 10;
var grow = '[';
while (count > 1)
{
grow = grow + count;
count = count - 1;
}
grow = grow + ']';
$('#putmehere').html(grow);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.