你好,这是测试用例,我必须在函数中传递n个参数

hello this is the test case and i have to pass n number of arguments in function

本文关键字:参数 函数 测试用例 你好      更新时间:2023-09-26

你好,这是测试用例,我必须在函数中传递n个参数(‘你应该能够使用参数’,

    function () {   
        var a = Math.random(),    
           b = Math.random(),   
           c = Math.random(),  
             d = Math.random(); 
           expect(answers.useArguments(a)).to.eql(a);    
   expect(answers.useArguments(a, b)).to.eql(a + b);      
 expect(answers.useArguments(a, b, c)).to.eql(a + b + c);      
 expect(answers.useArguments(a, b, c, d)).to.eql(a + b + c + d);   
      });

所以在函数中,我必须写什么,我写这个

return Array.prototype.call(arguments,function(a,b)                                         {         
      for(var i=0;i<arguments.length;i++)                  {   
                   return a+b;                
  }          
   });

这里有一个函数来求和它的参数:

function sum() {
    var result = 0;
    for(var i=0; i<arguments.length; i++) {
        result += arguments[i];
    }
    return result;
}

或使用reduce:

function sum() {
    return [].slice.call(arguments).reduce(function(a,b) {
        return a+b;
    });
}