简体   繁体   English

Javascript:脚本死于函数调用

[英]Javascript: Script dies on Function Call

I'm currently studying javascript from http://eloquentjavascript.net/ . 我目前正在从http://eloquentjavascript.net/学习javascript。 I have made a habit of going through the given exercises before moving on to the next chapter to get some practice. 在进入下一章进行练习之前,我已经习惯了进行给定的练习。

So I've just finished reading chapter 4, and was going through the exercises given at the end of the chapter. 因此,我刚读完第4章,并正在完成本章末尾给出的练习。 Link: http://eloquentjavascript.net/04_data.html 链接: http//eloquentjavascript.net/04_data.html

I've just started the first exercise question. 我刚刚开始第一个练习题。

I have successfully completed the first 2 portions of the first exercise question which are: 我已经成功完成了第一个练习问题的前两个部分:

Write a range function that takes two arguments, start and end, and returns an array containing all the numbers from start up to (and including) end. 编写一个范围函数,该函数接受开始和结束两个参数,并返回一个数组,其中包含从开始到(包括)结束的所有数字。

Next, write a sum function that takes an array of numbers and returns the sum of these numbers. 接下来,编写一个求和函数,该函数采用一个数字数组并返回这些数字的总和。 Run the previous program and see whether it does indeed return 55. 运行前面的程序,查看它是否确实返回55。

using the following code: 使用以下代码:

// Your code here.
function range(start, end)
{
  var rangearray = new Array();
  for(  var i = start ; (i <= end + 1)  ;   i++)
    rangearray.push(i);
  return rangearray;
}

function sum(numarray)
{
  var result = 0;
  //var numb = 0;
  for(numb in numarray)
    result += parseInt(numb);
  return result;
}


console.log(sum(range(1, 10)));
// → 55  (this is supposed to be the output, and I get this without any problem)

However, there is a bonus task to the same exercise, which seems fairly simple, but I'm failing at it miserably: 但是,在同一练习中有一个额外的任务,看似相当简单,但我却惨败于此:

As a bonus assignment, modify your range function to take an optional third argument that indicates the “step” value used to build up the array. 作为奖励分配,修改您的范围函数以采用可选的第三个参数,该参数指示用于构建数组的“ step”值。 If no step is given, the array elements go up by increments of one, corresponding to the old behavior. 如果未给出任何步骤,则数组元素将以1的增量递增,这与旧的行为相对应。 The function call range(1, 10, 2) should return [1, 3, 5, 7, 9]. 函数调用范围(1、10、2)应该返回[1、3、5、7、9]。 Make sure it also works with negative step values so that range(5, 2, -1) produces [5, 4, 3, 2]. 确保它也适用于负步进值,以便range(5,2,-1)产生[5,4,3,2]。

Here is the code I am using: 这是我正在使用的代码:

// Your code here.
function range(start, end, step)
{
  var rangearray = new Array();
  end = (end < start) ? end - 1: end + 1;
  step = (typeof step === 'undefined') ? 1 : parseInt(step);
  var i = 0;
  for(  i = start ; ((step < 0) ? (i >= end) : (i <= end)) ; (i + step) )
  {rangearray.push(i);}
  return rangearray;
}

function sum(numarray)
{
  var result = 0;
  //var numb = 0;
  for(numb in numarray)
    result += parseInt(numb);
  return result;
}

console.log(range(5, 2, -1));


// → [5, 4, 3, 2]    (this is supposed to be the output)

When the code runs, I get an alert message saying that the code has been running for more than 2 seconds, and asks if it should be aborted. 代码运行时,我收到一条警告消息,指出代码已运行2秒钟以上,并询问是否应中止该代码。 The same thing happens after 10 seconds. 10秒后会发生相同的情况。 On aborting, the following error is received: 在中止时,收到以下错误:

Error: Aborted (line 204 in function tick) 错误:已中止(函数刻度中的第204行)

called from line 9 in function range 从函数范围的第9行调用

called from line 25 从第25行呼叫

Any guidance would be much appreciated. 任何指导将不胜感激。 :) :)

you are not incrementing the i with 你没有用

 (i + step)

it should be 它应该是

 i += step 
 // or 
 i = i + step
 // is the same

So, the result is, that the i never gets bigger, so it's an infinite loop.. browser crashes 因此,结果是,我永远不会变大,所以这是一个无限循环..浏览器崩溃

The reason you are getting this message is because you've successfully created your first infinite loop! 收到此消息的原因是因为您已经成功创建了第一个无限循环! But don't feel bad, this is a very common problem. 但是不要感到难过,这是一个非常普遍的问题。 The for loop you created will never actually come to a close. 您创建的for循环实际上永远不会结束。 This is because you are not incrementing "i" so it will never become greater than or equal to end. 这是因为您没有增加“ i”,所以它永远不会大于或等于end。 Instead you are just doing some math which the computer is more than glad to do. 相反,您只是在做一些数学,计算机对此很高兴。 Instead of "+", you need "+=" to actually assign "i" to be itself plus whatever "step" is. 代替“ +”,您需要“ + =“来实际分配“ i”作为自身加上“ step”是什么。 Also, I took out your ternary operator for "end" because I simply didn't not see the reason for it. 另外,我掏出了您的三元运算符来表示“结束”,因为我根本不知道这样做的原因。 Your code before was just fine. 您之前的代码还不错。 I hope you are enjoying JavaScript! 希望您喜欢JavaScript!

One last suggestion is to never put a ternary operator in a for loop. 最后一个建议是永远不要将三元运算符放在for循环中。

// Your code here.
function range(start, end, step)
{
    var rangearray = new Array();
    step = (typeof step === 'undefined') ?     
                           1 : parseInt(step);
    var i = 0;
    for(  i = start; i <= end ; (i += step) )
        {rangearray.push(i);}
    return rangearray;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM