0

I'm currently studying javascript from http://eloquentjavascript.net/. 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. Link: 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.

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. If no step is given, the array elements go up by increments of one, corresponding to the old behavior. The function call range(1, 10, 2) should return [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].

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. The same thing happens after 10 seconds. On aborting, the following error is received:

Error: Aborted (line 204 in function tick)

called from line 9 in function range

called from line 25

Any guidance would be much appreciated. :)

5
  • 1
    line 204 in function tick ... you didn't think to post the code around that? Commented Sep 9, 2015 at 11:23
  • the site uses sandbox?!? to allow you to put your code in a code box and run it right there (on the site)... and the code I've posted is the only bit of code that was written in the available box. There's no tick function there in the box Commented Sep 9, 2015 at 11:27
  • 1
    never mind, I see the issue, i never changes, so, infinite loop ... i + step should be i += step Commented Sep 9, 2015 at 11:27
  • thanks that worked. Not sure how I missed that -_-' Commented Sep 9, 2015 at 11:32
  • @shaz Maybe consider using a while loop, I think that would be a lot clearer and easier to overlook. Commented Sep 9, 2015 at 11:37

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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. This is because you are not incrementing "i" so it will never become greater than or equal to 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. 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!

One last suggestion is to never put a ternary operator in a for loop.

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

2 Comments

Well actually, the ternary operator for end is required, as the range could be in the reverse order too, like it states in the quoted question. That bit is added to ensure it can handle loop iteration when end is less than start.Thanks for your input, but the question has already been answered. :)
Ha. Completely missed that last part. I would still suggest swapping start and end's values depending on if step is negative though. Ternary operators can cause random infinite loops and make the for loop harder to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.