javascript - 三元运算符不返回未定义
问题描述
我正在研究 FCC 中间算法“Arguments Optional”。以下是关于需要发生什么的说明:
中间算法脚本:参数可选
- 创建一个将两个参数相加的函数。如果只提供一个参数,则返回一个函数,该函数需要一个参数并返回总和。
- 例如,addTogether(2, 3) 应该返回 5,addTogether(2) 应该返回一个函数。
- 使用单个参数调用此返回函数将返回总和: var sumTwoAnd = addTogether(2); sumTwoAnd(3) 返回 5。
- 如果任一参数不是有效数字,则返回 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.
解决方案
这是因为您必须检查过滤器的输入参数和输出参数。尝试添加此代码段:
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"));
推荐阅读
- swift - 泛型方法的类型不匹配问题
- asp.net - 使用 React 和 ASP 下载文件
- haskell - 在 Haskell 中手动实现整数除法
- python - 用深拷贝复制类会以某种方式导致无限递归
- asp.net - 无法在 RDLC 报告生成的 pdf 中搜索阿拉伯语单词
- php - PHP Delete API request, using curl: Bad Request Your browser send a request that this server could not understand
- elasticsearch - Searchkick / Elasticsearch 错误:请改用关键字字段。或者,在 [name] 上设置 fielddata=true
- reactjs - react-router-dom v5 - 链接有效,但 Redirect 和 history.push() 无效
- c++ - 如何在 Linux 中检测窗口调整大小
- ethereum - 如何使用 Ethers.js 从代理合约的后备功能中获取响应数据