Pages

Monday, August 29, 2016

Recursion exercise 3

By the way, I stole these problems from : http://www.w3resource.com/javascript-exercises/javascript-recursion-functions-exercises.php


Write a recursive function to get integers in a range.

For example, range(2,7)    will return [3,4,5,6];

Yes, that is returned in an array.

Answer is in the comments.

1 comment:

  1. var range = function(start_num, end_num)
    {
    if (end_num - start_num === 2)
    {
    return [start_num + 1];
    }
    else
    {
    var list = range(start_num, end_num - 1);
    list.push(end_num - 1);
    return list;
    }
    };

    ReplyDelete