首页 > 解决方案 > 三元运算符不返回未定义

问题描述

我正在研究 FCC 中间算法“Arguments Optional”。以下是关于需要发生什么的说明:

中间算法脚本:参数可选

  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.

标签: javascriptalgorithmundefinedconditional-operator

解决方案


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

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"));


推荐阅读