【问题标题】:Javascript : The sum of its rangeJavascript : 其范围的总和
【发布时间】:2016-10-10 02:32:04
【问题描述】:

我目前正在学习 Eloquent Javascript 中的练习,但在一个练习中有点卡住了。让我用代码解释一下:

var _numberArray = [];

function range(start, end, step) 
{
  console.log(_numberArray); 


  if (!step)
  {
    step = 1;
  }

  if (step < 0)
  {
     for(var i = start; i >= end; i -= step) 
     {
        _numberArray.push(i);
        console.log(_numberArray);


     }
  }
  else 
  {

     for(var i = start; i <= end; i += step) 
     {
        _numberArray.push(i);
        console.log(_numberArray);
     }
  }


    return _numberArray;
}


  function sum(array) 
  {
    var total = 0; 
    for (var i = 0; i < array.length; i++)
    {

        total += array[i];

    }
  return total;

}

console.log(sum(range(42,14,-2)));

所以我创建了 range 函数,用于检查 step 参数是否已设置。如果不是,则将默认步长变量设置为 1。如果步长小于 0,请确保在第一个参数大于第二个参数的情况下循环,并根据给定的步长减小值。这与递增相同。

但是,只有当我使用负值时,此代码才会导致崩溃。因此,当我执行“sum(range(22,6,-2));”时代码崩溃。反过来“sum(range(6,22,2));”它确实有效。当我更换时:

  for(var i = start; i >= end; i -= step) 
         {
            _numberArray.push(i);
            console.log(_numberArray);


         }

为:

    for(var i = start; i >= end; i--) 
     {
        _numberArray.push(i);
        console.log(_numberArray);


     }

它再次起作用了!有人可以帮助我并让我了解它为什么崩溃以及为什么最后一种方法有效吗?

谢谢。

【问题讨论】:

    标签: javascript for-loop


    【解决方案1】:

    由于步长值为负,并且您正在向下迭代,因此您需要添加它,而不是减去它:

    for(var i = start; i >= end; i += step) 
    

    减去 -2 与加 2 相同,因此您将得到 42、44、46,...并且该值始终大于“end” (14),因此是无限循环。

    【讨论】:

      【解决方案2】:
      if (step < 0)
         {
           for(var i = start; i >= end; i -= step)
      

      如果说 step 为负 2,start 为 22,end 为 6:

      i = 22, 22 >= 6, i = 22 - (-2)
      i = 24, 24 >= 6, i = 24 - (-2)
      i = 26...
      

      看起来您将负步数计算了两次(该步数为负数,因此您不需要减去它)。

      【讨论】:

        【解决方案3】:

        我想出了一个解决方案来解决负数和负步骤的问题,还修改了一些函数,所以你没有两个 for 循环,而只是在 while 循环上。

            function range(n1,n2,step){
              var start, end;
              var list = [];
        
            /*
            Function take two parameters and decides which will become 
            the start by comparing both and evaluating who is smaller.
            I used the ternary operator to test an expression if true
            the first option is applied else the second option.
            */
              (n1 > n2 ? (start = n2, end = n1):(start = n1, end = n2));
            //Check if step is null or zero
              if(step == null || step == 0)
                step = 1;
            /*
            Check if step is a negative number, if so convert it to a 
            postive number. Since the start will always be the smallest 
            number there is no need to subtract even if the var start and
            var end are both negative numbers
            */
              if(step < 0)
                step *= -1;
            // Use a while loop to push numbers into array    
              while(start <= end){
                list.push(start);
                start += step;
              }
            // Made sure the var end was always included at the end of the list    
              if(list.indexOf(end) == -1)
                list.push(end);
        
            // Return list and end program
            return list;
            }
        
            //funciton that adds up the list of numbers 
        
            function sum(array){
              var total = 0 ;
              for(var i = 0; i < array.length; i++){
                total += array[i];
              }
              return total;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-29
          • 1970-01-01
          • 2023-01-26
          • 1970-01-01
          • 2015-08-31
          • 1970-01-01
          • 2015-04-26
          • 2013-08-03
          相关资源
          最近更新 更多