10 KiB
10 KiB
title | localeTitle |
---|---|
Arguments Optional | 参数可选 |
如果卡住,请记得使用**Read-Search-Ask
** 。尝试配对程序并编写自己的代码
问题说明:
理解需要做什么可能会非常复杂。在编码时总是有很多方法可以做,但无论使用何种算法,我们都必须创建一个执行以下操作的程序:
- 它必须添加两个作为参数传递的数字并返回总和。
- 它必须检查是否有任何数字是实际数字,否则返回undefined并在那里停止程序。
- 它必须检查它是否传递了一个或两个参数。更多被忽略。
- 如果它只有一个参数,那么它必须返回一个使用该数字并期望另一个的函数,然后添加它。
相关链接
提示:1
每次处理参数时,都必须检查它是否为数字。为此,处理此任务的函数将为您保存重复的代码。
现在尝试解决问题
提示:2
当处理它需要返回函数的情况时,最好再次检查第一个和唯一的参数是否为数字,并将代码作为基础。
现在尝试解决问题
提示:3
在只传递了一个参数的情况下,不要担心如何提示输入第二个参数,只需正确定义函数定义,事情就会按照应有的方式运行。
现在尝试解决问题
扰流警报!
提前解决!
基本代码解决方案
function addTogether() {
// Function to check if a number is actually a number
// and return undefined otherwise.
var checkNum = function(num) {
if (typeof num !== 'number') {
return undefined;
} else
return num;
};
// Check if we have two parameters, check if they are numbers
// handle the case where one is not
// returns the addition.
if (arguments.length > 1) {
var a = checkNum(arguments[0]);
var b = checkNum(arguments[1]);
if (a === undefined || b === undefined) {
return undefined;
} else {
return a + b;
}
} else {
// If only one parameter was found, returns a new function that expects two
// Store first argument before entering the new function scope
var c = arguments[0];
// Check the number again, must be outside the function to about returning an object
// instead of undefined.
if (checkNum(c)) {
// Return function that expect a second argument.
return function(arg2) {
// Check for non-numbers
if (c === undefined || checkNum(arg2) === undefined) {
return undefined;
} else {
// if numbers then add them.
return c + arg2;
}
};
}
}
}
// test here
addTogether(2,3);
代码说明:
- 首先,我创建一个函数,其唯一目的是检查数字是否实际上是一个数字,如果不是则返回undefined。它使用typeof来检查。
- 检查我们是否有两个参数,如果有,然后使用我创建的checkNum函数检查它们是否为数字。
- 如果它们**未定义,**则添加它们并返回添加。如果它们中的任何一个未定义,则返回undefined。
- 在我们只有一个参数的情况下,我们返回一个需要两个参数的新函数。为此,我们在进入新范围之前存储第一个参数,以避免我们的参数被覆盖。
- 仍然在大的其他内部,我们需要检查我们保存的参数,如果它是一个数字,那么我们返回期望第二个参数的函数。
- 现在在我们返回的函数内部,我们必须再次检查非数字,就像在开始时使用**checkNum一样,**如果未定义则返回,否则如果数字添加它们并返回添加。
相关链接
中级代码解决方案:
function addTogether() {
var args = new Array(arguments.length);
//Storing the arguments in an array
for(var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
//Check for the arguments length
if(args.length == 2){
//If there are two arguments,check for the type of both arguments
//Use typeof to check the type of the argument(both should be numbers)
if(typeof args[0] !== 'number' || typeof args[1] !=='number' ){
return undefined;
}
return args[0]+args[1];
}
//When only one argument is provided
if(args.length == 1){
a= args[0];
//Check the argument using typeof
if(typeof a!=='number'){
return undefined;
}
else{
//Making use of closures
return function(b){
//Checking the second argument
if(typeof b !=='number'){
return undefined;
}
else
return a+b;
};
}
}
}
// test here
addTogether(2,3);
代码说明:
- 首先使用构造函数方法创建数组,将参数存储在数组中。
- 将每个参数添加到新数组。
- 然后检查新阵列的长度,因为我们需要知道是否有足够的数据。
- 使用
typeof
检查参数的类型,因为它们都应该是数字。 - 如果它们中的任何一个不是数字,则返回undefined,如果它们是,则返回它们的总和。
- 如果只有一个参数,我们仍然会在将其存储到新变量并返回新函数或未定义之后检查类型。
相关链接
高级代码解决方案
//jshint esversion: 6
function addTogether() {
var args = Array.from(arguments);
return args.some(n => typeof n !== 'number') ?
undefined:
args.length > 1 ?
args.reduce((acc, n) => acc += n, 0):
(n) => typeof n === "number" ?
n + args[0]:
undefined;
}
// test here
addTogether(2,3);
代码说明:
- 首先,我遍历参数并检查不是数字的参数并返回undefined
- 如果不是我,那么检查参数长度是否大于1,如果是,我使用Array.prototype.reduce对参数求和
- 否则我返回一个函数,检查传入的参数是否是一个数字并加总,如果没有返回undefined
相关链接
注意:如果您已将任何相关主要内容添加到Wiki页面,请仅添加您的用户名。 (请不要删除任何现有的用户名。)
捐款说明:
- 请勿添加与任何现有解决方案类似的解决方案。如果您认为它**相似但更好** ,那么尝试合并(或替换)现有的类似解决方案。
- 添加解决方案的说明。
- 将解决方案分为以下类别之一 - 基本 , 中级和高级 。
- 如果您添加了任何**相关的主要内容,**请仅添加您的用户名。 ( 不要 删除任何现有的用户名 )