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";
}
添加回答
举报