三元运算符不返回未定义
Ternary operator is not returning undefined
我正在研究 FCC 中间算法 "Arguments Optional"。以下是有关需要发生的事情的说明:
Intermediate Algorithm Scripting: Arguments Optional
- 创建一个将两个参数相加的函数。如果只提供一个参数,那么 return 一个函数需要一个参数并且 return 是总和。
- 例如,addTogether(2, 3) 应该 return 5,而 addTogether(2) 应该 return 一个函数。
- 使用单个参数调用此 returned 函数将 return 求和:
var sumTwoAnd = addTogether(2);
sumTwoAnd(3) returns 5.
- 如果任一参数不是有效数字,return未定义。
我写了代码来完成上面解释的所有事情,但一个要求是参数必须全部是数字,否则 return undefined
(上面的#4)。您会看到我写了一个三元运算符(代码的第 5 行)numbersOnly
变量,我认为它可以处理这个问题,但它只是在控制台中 returning [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,它应该 return 未定义。我不太明白为什么 5 会通过而 returning 未定义但 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.
因为过滤参数时,只过滤掉'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);
这是因为您必须检查过滤器的输入参数和输出参数。
尝试添加此代码段:
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"));
我正在研究 FCC 中间算法 "Arguments Optional"。以下是有关需要发生的事情的说明:
Intermediate Algorithm Scripting: Arguments Optional
- 创建一个将两个参数相加的函数。如果只提供一个参数,那么 return 一个函数需要一个参数并且 return 是总和。
- 例如,addTogether(2, 3) 应该 return 5,而 addTogether(2) 应该 return 一个函数。
- 使用单个参数调用此 returned 函数将 return 求和: var sumTwoAnd = addTogether(2); sumTwoAnd(3) returns 5.
- 如果任一参数不是有效数字,return未定义。
我写了代码来完成上面解释的所有事情,但一个要求是参数必须全部是数字,否则 return undefined
(上面的#4)。您会看到我写了一个三元运算符(代码的第 5 行)numbersOnly
变量,我认为它可以处理这个问题,但它只是在控制台中 returning [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,它应该 return 未定义。我不太明白为什么 5 会通过而 returning 未定义但 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.
因为过滤参数时,只过滤掉'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);
这是因为您必须检查过滤器的输入参数和输出参数。 尝试添加此代码段:
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"));