2 回答

TA貢獻1810條經驗 獲得超4個贊
這段代碼將完全按照您的要求進行:
function findMyCampsites(campgrounds, string, number) {
const campsites = [];
for(let i = 0; i < campgrounds.length; i++) {
if(
campgrounds[i].isReserved === false &&
campgrounds[i].view === string &&
campgrounds[i].partySize >= number
) {
campsites.push(campgrounds[i].number);
}
}
if (campsites.length === 0) {
return "Sorry, no campsites with that view are available to host your party";
} else {
return campsites;
}
}
但是,我想強調幾件事,首先,您希望使函數參數更具描述性。即,不要調用第二個參數string,可以將其稱為類似的名稱siteType,而不是第三個參數number,嘗試類似的名稱requiredSpaces。另外,這個函數返回一個字符串(如果沒有找到站點)和一個數組(如果找到站點)有點奇怪。通常,您通常希望該函數僅返回單一類型,但如果這就是簡報要求您做的,那就這樣吧。在您的編碼職業生涯中,有幾件事需要您考慮。

TA貢獻1873條經驗 獲得超9個贊
function findMyCampsites(campgrounds, string, number) {
const campsites = [];
for(let i = 0; i < campgrounds.length; i++) {
if(
campgrounds[i].isReserved === false &&
campgrounds[i].view === string &&
campgrounds[i].partySize >= number
) {
campsites.push(campgrounds[i].number);
}
}
return array.length
? array
: "Sorry, no campsites with that view are available to host your
party";
}
添加回答
舉報