freeCodeCamp/guide/chinese/certifications/javascript-algorithms-and-d.../intermediate-algorithm-scri.../arguments-optional/index.md

10 KiB
Raw Blame History

title localeTitle
Arguments Optional 参数可选

:triangular_flag_on_post:如果卡住,请记得使用**Read-Search-Ask** 。尝试配对程序:busts_in_silhouette:并编写自己的代码:pencil:

:checkered_flag:问题说明:

理解需要做什么可能会非常复杂。在编码时总是有很多方法可以做,但无论使用何种算法,我们都必须创建一个执行以下操作的程序:

  • 它必须添加两个作为参数传递的数字并返回总和。
  • 它必须检查是否有任何数字是实际数字,否则返回undefined并在那里停止程序。
  • 它必须检查它是否传递了一个或两个参数。更多被忽略。
  • 如果它只有一个参数,那么它必须返回一个使用该数字并期望另一个的函数,然后添加它。

相关链接

:speech_balloon:提示1

每次处理参数时,都必须检查它是否为数字。为此,处理此任务的函数将为您保存重复的代码。

现在尝试解决问题

:speech_balloon:提示2

当处理它需要返回函数的情况时,最好再次检查第一个和唯一的参数是否为数字,并将代码作为基础。

现在尝试解决问题

:speech_balloon:提示3

在只传递了一个参数的情况下,不要担心如何提示输入第二个参数,只需正确定义函数定义,事情就会按照应有的方式运行。

现在尝试解决问题

扰流警报!

警告牌

提前解决!

:beginner:基本代码解决方案

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

:rocket: 运行代码

代码说明:

  • 首先我创建一个函数其唯一目的是检查数字是否实际上是一个数字如果不是则返回undefined。它使用typeof来检查。
  • 检查我们是否有两个参数,如果有,然后使用我创建的checkNum函数检查它们是否为数字。
  • 如果它们**未定义,**则添加它们并返回添加。如果它们中的任何一个未定义则返回undefined。
  • 在我们只有一个参数的情况下,我们返回一个需要两个参数的新函数。为此,我们在进入新范围之前存储第一个参数,以避免我们的参数被覆盖。
  • 仍然在大的其他内部,我们需要检查我们保存的参数,如果它是一个数字,那么我们返回期望第二个参数的函数。
  • 现在在我们返回的函数内部,我们必须再次检查非数字,就像在开始时使用**checkNum一样**如果未定义则返回,否则如果数字添加它们并返回添加。

相关链接

:sunflower:中级代码解决方案:

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

:rocket: 运行代码

代码说明:

  • 首先使用构造函数方法创建数组,将参数存储在数组中。
  • 将每个参数添加到新数组。
  • 然后检查新阵列的长度,因为我们需要知道是否有足够的数据。
  • 使用typeof检查参数的类型,因为它们都应该是数字。
  • 如果它们中的任何一个不是数字则返回undefined如果它们是则返回它们的总和。
  • 如果只有一个参数,我们仍然会在将其存储到新变量并返回新函数或未定义之后检查类型。

相关链接

:rotating_light:高级代码解决方案

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

:rocket: 运行代码

代码说明:

  • 首先我遍历参数并检查不是数字的参数并返回undefined
  • 如果不是我那么检查参数长度是否大于1如果是我使用Array.prototype.reduce对参数求和
  • 否则我返回一个函数检查传入的参数是否是一个数字并加总如果没有返回undefined

相关链接

注意:如果您已将任何相关主要内容添加到Wiki页面请仅添加您的用户名。 (请不要删除任何现有的用户名。)

:clipboard:捐款说明:

  • :warning: 请勿添加与任何现有解决方案类似的解决方案。如果您认为它**相似但更好** ,那么尝试合并(或替换)现有的类似解决方案。
  • 添加解决方案的说明。
  • 将解决方案分为以下类别之一 - 基本 中级高级:traffic_light:
  • 如果您添加了任何**相关的主要内容,**请仅添加您的用户名。 :warning: 不要 删除任何现有的用户名

看到:point_right: Wiki Challenge Solution Template供参考。