本文介绍了是否可以降低此Javascript算法解决方案的复杂性和意大利面条的质量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:
创建一个将两个参数加在一起的函数。
如果只提供一个参数,则返回一个期望一个参数并返回总和的函数。

Problem:Create a function that sums two arguments together.If only one argument is provided, then return a function that expects one argument and returns the sum.

例如,addTogether(2,3)应该返回5,并且addTogether(2)应该返回一个函数。

For example, addTogether(2, 3) should return 5, and addTogether(2) should return a function.

使用单个参数调用此返回函数将返回总和:
var sumTwoAnd = addTogether( 2);
sumTwoAnd(3)返回5。

Calling this returned function with a single argument will then return the sum:var sumTwoAnd = addTogether(2);sumTwoAnd(3) returns 5.

如果任一自变量不是有效数字,则返回undefined。

If either argument isn't a valid number, return undefined.

解决方案应返回:

addTogether(2,3)应返回5。
addTogether(2)(3)应返回5。
addTogether(2, 3)应该返回未定义。
addTogether(2)([3])应该返回未定义。

addTogether(2, 3) should return 5.addTogether(2)(3) should return 5.addTogether(2, "3") should return undefined.addTogether(2)([3]) should return undefined.

我尽了一切努力,但唯一可行的方法,据说是最好的方法到目前为止的解决方案如下:

I tried everything I could, but the only thing that worked, and is purportedly the best solution so far is the following:

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代码的语义:

The following improvements have been suggested, but they would change the semantics of OPs code:


  • 如果未定义 > a 或 b NaN ,如 NaN 不是有效数字

  • 返回 undefined (如果提供了两个以上的参数,而不是静默地删除它们) (感谢@PatrickRoberts)

  • return undefined if a or b is NaN, as NaN is not a 'valid number'
  • return undefined if more than two arguments are provided instead of silently dropping them (thanks @PatrickRoberts)

如果您不介意为e返回函数。 G。 addTogether('x'),使用:

If you don't mind returning a function for e. g. addTogether('x'), use:

function addTogether(a, b) {
  if (arguments.length == 1) {
    return b => addTogether(a, b);
  } else if (typeof a == "number" && typeof b == "number") {
    return a + b;
  }
}

这样,您将始终返回 function 表示一个参数,而 Number undefined 表示两个或多个参数=更加强大的代码。

This way, your will always return a function for one argument and Number or undefined for two or more arguments = more robust code.

为了实现ES5兼容性,如果您不介意 addTogether(2)()返回函数,替换 b => addTogether(a,b) addTogether.bind(undefined,a)(感谢@PatrickRoberts)。

For ES5 compatibility and if you don't mind addTogether(2)() returning a function, replace b => addTogether(a, b) with addTogether.bind(undefined, a) (thanks @PatrickRoberts).

这篇关于是否可以降低此Javascript算法解决方案的复杂性和意大利面条的质量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 11:26