三元运算符未返回未定义

时间:2020-05-02 15:51:33

标签: javascript algorithm undefined conditional-operator

我正在研究FCC中间算法“可选参数”。这是有关需要发生的情况的说明:

中间算法脚本:参数可选

  1. 创建一个将两个参数加在一起的函数。如果仅提供一个参数,则返回一个期望一个参数并返回总和的函数。
  2. 例如,addTogether(2,3)应该返回5,而addTogether(2)应该返回一个函数。
  3. 使用单个参数调用此返回的函数将返回总和: var sumTwoAnd = addTogether(2); sumTwoAnd(3)返回5。
  4. 如果任一自变量都不是有效数字,则返回undefined。

我写了代码来完成上面解释的所有事情,但是一个要求是参数必须全部是数字,否则返回undefined(上面的#4)。您会看到我写了一个三元运算符(我的代码的第5行)numbersOnly变量,我认为它可以处理此变量,但这只是在控制台中返回[Function]。

function addTogether() {
    // Convert args to an array
    let args = [...arguments];
    // Only accept numbers or return undefined and stop the program
    const numbersOnly = value => typeof(value) === 'number'? value : undefined;
    // test args for numbersOnly and return only the first two arguments regardless of the length of args
    let numbers = args.filter(numbersOnly).slice(0, 2);

    // // It has to add two numbers passed as parameters and return the sum.
    if (numbers.length > 1) {
        return numbers[0] + numbers[1];
    }
    // If it has only one argument then it has to return a function that uses that number and expects another one, to then add it.
    else if (numbers.length === 1) {
        let firstParam = numbers[0];
        return function(secondParam) {
            if (typeof secondParam !== 'number' || typeof firstParam !== 'number') {
                return undefined;
            }
            return secondParam + firstParam;
        }
    }
}

我正在通过所有测试,但#4除外,该测试应返回未定义。我不太明白为什么5通过并返回undefined但4失败。我在这里想念什么?谢谢!

1. addTogether(2, 3) should return 5.
2. addTogether(2)(3) should return 5.
3. addTogether("https://www.youtube.com/watch?v=dQw4w9WgXcQ") should return undefined.
4. addTogether(2, "3") should return undefined.
5. addTogether(2)([3]) should return undefined.

2 个答案:

答案 0 :(得分:1)

这是因为您必须检查过滤器的输入参数和输出参数。 尝试添加以下代码段:

let numbers = args.filter(numbersOnly).slice(0, 2);
if (args.length > numbers.length) {
  return undefined;
}

function addTogether() {
    // Convert args to an array
    let args = [...arguments];
    // Only accept numbers or return undefined and stop the program
    const numbersOnly = value => typeof(value) === 'number'? value : undefined;
    // test args for numbersOnly and return only the first two arguments regardless of the length of args
    let numbers = args.filter(numbersOnly).slice(0, 2);
    if (args.length > numbers.length) {
      return undefined;
    }

    // // It has to add two numbers passed as parameters and return the sum.
    if (numbers.length > 1) {
        return numbers[0] + numbers[1];
    }
    // If it has only one argument then it has to return a function that uses that number and expects another one, to then add it.
    else if (numbers.length === 1) {
        let firstParam = numbers[0];
        return function(secondParam) {
            if (typeof secondParam !== 'number' || typeof firstParam !== 'number') {
                return undefined;
            }
            return secondParam + firstParam;
        }
    }
  }
  console.log('4. addTogether', addTogether(4, "4"));

答案 1 :(得分:0)

由于过滤参数时,只有'3'被过滤掉,因此剩余数组的长度为1。

const args = [2, '3'];
const numbers = args.filter((n) => typeof n === 'number');
console.log('numbers:', numbers);
console.log('numbers.length === 1:', numbers.length === 1);