[英]A question about freeCodeCamp Challenge : Arguments Optional
我堅持使用 freeCodeCamp.org https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional 中的課程之一
下面的代碼是我寫的。 在該代碼中,addTogether(2)(3) 應該是 5。但不是那個,addTogether(2)(3) 是“未定義的”
有什么問題?
我閱讀了 freecodecamp 論壇的所有提示。 但我不明白。
function addTogether() { var checkNum = function(x) { if (typeof x === "number") { return x } else { return undefined } } if (arguments.length > 1) { if (checkNum(arguments[0]) !== undefined && checkNum(arguments[1]) !== undefined) { return arguments[0] + arguments[1] } else { return undefined } } else { var a = arguments[0] if (checkNum(a) === undefined) { return undefined } else { return function(args2) { args2 + a } } } return false; } console.log(addTogether(2)(3))
您返回的函數沒有返回值。 你可以用
return function(args2) {
return args2 + a
}
或者
return (args2) => args2 + a
function addTogether(a,b) {
if (arguments.length==2){
if (typeof a == "number" && typeof b == "number"){
return a + b;
}
}
if(arguments.length==1){
if (typeof a == "number"){
return function(b){
if (typeof b == "number"){
return a + b;
}
};
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.