[英]A question about freeCodeCamp Challenge : Arguments Optional
I'm stuck with one of curriculum in freeCodeCamp.org https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional我坚持使用 freeCodeCamp.org https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional 中的课程之一
The below code is what I wrote.下面的代码是我写的。 In that code, addTogether(2)(3) should be 5. But instead of that, addTogether(2)(3) is "undefined"在该代码中,addTogether(2)(3) 应该是 5。但不是那个,addTogether(2)(3) 是“未定义的”
what's the problem?有什么问题?
I read all hints from freecodecamp forum.我阅读了 freecodecamp 论坛的所有提示。 But I don't get it.但我不明白。
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))
Your returned function doesn't have a return value.您返回的函数没有返回值。 You could use你可以用
return function(args2) {
return args2 + a
}
Or或者
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.