[英]callbacks when trying to create an array from asynchronous calls in javascript
[英]Error when trying to return a string with calls from array
此代碼返回錯誤和第二個“else if”語句:
function likes(names) { if (names.length == 0) { return "no one likes this" } else if (names.length == 1) { return names[0] + " likes this" }; // the above is running fine if I remove the second to else if statements. else if (names.length == 2) { return names[0] + " and " /*error starts here*/ + names[0] + " like this" }; else if (names.length == 3) { return names[0] + ", " + names[1] + " and " + names[2] + " like this" }; else if (names.lenght > 3) { return names[0] + ", " + names[1] + " and " + names.length - 1 + "others like this}; } console.log(likes(["james", "pete"]))
我想這是我如何將琴弦連接在一起的問題,但我似乎無法弄清楚。
不好意思,還在學習。 感謝任何反饋。
您正在添加;
在else if
它破壞了代碼。 同樣在這一行中沒有關閉"
return names[0] + ", " + names[1] + " and " + names.length - 1 + "others like this}
function likes(names) { if (names.length == 0) { return "no one likes this" } else if (names.length == 1) { return names[0] + " likes this" } // the above is running fine if I remove the second to else if statements. else if (names.length == 2) { return names[0] + " and " /*error starts here*/ + names[0] + " like this" } else if (names.length == 3) { return names[0] + ", " + names[1] + " and " + names[2] + " like this" } else if (names.lenght > 3) { return names[0] + ", " + names[1] + " and " + names.length - 1 + "others like this" } } console.log(likes(["james", "pete"]))
問題:
};
"
function likes(names) { if (names.length == 0) { return "no one likes this" } else if (names.length == 1) { return names[0] + " likes this" } else if (names.length == 2) { return names[0] + " and " /*error starts here*/ + names[0] + " like this" } else if (names.length == 3) { return names[0] + ", " + names[1] + " and " + names[2] + " like this" } else if (names.lenght > 3) { return names[0] + ", " + names[1] + " and " + (names.length - 1) + "others like this" } } console.log(likes(["james", "pete"]))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.