是否可以降低此Javascript算法解决方案的复杂性和意大利面条的质量?
作者:互联网
问题:
创建一个将两个参数加在一起的函数.
如果仅提供一个参数,则返回一个期望一个参数并返回总和的函数.
例如,addTogether(2,3)应该返回5,而addTogether(2)应该返回一个函数.
然后使用单个参数调用此返回的函数将返回总和:
var sumTwoAnd = addTogether(2);
sumTwoAnd(3)返回5.
如果任何一个参数都不是有效数字,则返回undefined.
解决方案应返回:
addTogether(2,3)应该返回5.
addTogether(2)(3)应该返回5.
addTogether(2,“ 3”)应该返回未定义.
addTogether(2)([3])应该返回未定义.
我尽了我所能,但是唯一有效的方法是,到目前为止,最好的解决方法是:
function addTogether() {
"use strict";
// check if argument(s) valid number
var validateNum = function(num) {
if(typeof num !== 'number') {
return undefined;
} else
return num;
};
// is there is one argument or two
if(arguments.length > 1) {
var a = validateNum(arguments[0]);
var b = validateNum(arguments[1]);
if(a === undefined || b === undefined) {
return undefined;
} else {
return a + b;
}
// if only one argument, return function that expects one argument and returns sum.
} else {
var c = arguments[0];
// start here
if(validateNum(c)) {
return function(arg2) {
if(c === undefined || validateNum(arg2) === undefined) {
return undefined;
} else {
return c + arg2;
}
}; // belongs to return function(arg2) {}
}
}
}
addTogether(2)(3);
解决方法:
function addTogether(a, b) {
if (typeof a == "number") {
if (arguments.length == 1) {
return b => addTogether(a, b);
} else if (typeof b == "number") {
return a + b;
}
}
}
// as per OP's code
// returns 3
console.log("addTogether(1, 2) = " + addTogether(1, 2));
console.log("addTogether(1, 2, 3) = " + addTogether(1, 2, 3));
console.log("addTogether(1)(2) = " + addTogether(1)(2));
console.log("addTogether(1)(2, 3) = " + addTogether(1)(2, 3));
console.log("addTogether(1, 2, '3') = " + addTogether(1, 2, '3'));
console.log("addTogether(1)(2, '3') = " + addTogether(1)(2, '3'));
console.log("addTogether(1, 2, [3]) = " + addTogether(1, 2, [3]));
console.log("addTogether(1)(2, [3]) = " + addTogether(1)(2, [3]));
console.log("addTogether(1, 2, NaN) = " + addTogether(1, 2, NaN));
console.log("addTogether(1)(2, NaN) = " + addTogether(1)(2, NaN));
// returns NaN
console.log("addTogether(1, NaN) = " + addTogether(1, NaN));
console.log("addTogether(1)(NaN) = " + addTogether(1)(NaN));
// returns undefined
console.log("addTogether() = " + addTogether());
console.log("addTogether(1)() = " + addTogether(1)());
console.log("addTogether('1') = " + addTogether('1'));
console.log("addTogether(1, '2') = " + addTogether(1, '2'));
console.log("addTogether(1)('2') = " + addTogether(1)('2'));
console.log("addTogether(1, [2]) = " + addTogether(1, [2]));
console.log("addTogether(1)([2]) = " + addTogether(1)([2]));
提出了以下改进,但是它们将改变OPs代码的语义:
>如果a或b为NaN,则返回未定义,因为NaN不是“有效数字”
>如果提供了两个以上的参数,而不是默默地删除它们,则返回undefined(感谢@PatrickRoberts)
如果您不介意为e返回函数. G. addTogether(‘x’),使用:
function addTogether(a, b) {
if (arguments.length == 1) {
return b => addTogether(a, b);
} else if (typeof a == "number" && typeof b == "number") {
return a + b;
}
}
这样,您将始终为一个参数和Number返回一个函数,或者为两个或多个参数返回undefined =更可靠的代码.
为了实现ES5兼容性,并且如果您不介意addTogether(2)()返回函数,请替换b =>. addTogether(a,b)与addTogether.bind(undefined,a)(感谢@PatrickRoberts).
标签:complexity-theory,javascript 来源: https://codeday.me/bug/20191118/2028832.html