我想從使用JS的數組中提取URL,但我的代碼似乎沒有返回任何東西。編寫函數從數組中提取URL的難度
將不勝感激任何幫助!
var pages = [
"www.facebook.com|Facebook",
"www.twitter.com|Twitter",
"www.google.co.uk|Google"
];
function url1_m1(pages, pattern) {
var URL = '' // variable ready to accept URL
for (var i = 0; i < pages[i].length; i++) {
// for each character in the chosen page
if (pages[i].substr(i, 4) == "www.") {
// check to see if a URL is there
while (pages[i].substr(i, 1) != "|") {
// if so then lets assemble the URL up to the colon
URL = URL + pages[i].substr(i, 1);
i++;
}
}
}
return (URL);
// let the user know the result
}
alert(url1_m1(pages, "twitter")); // should return www.twitter.com
? –
對不起,這是一個錯字 - 我更新了代碼! – Johnny
哎唷!你的代碼充滿了錯誤,請在嘗試其他任何事情之前學習正確運行循環,請! –