sum, range and rangeSum functions Forgive me if this is too simple, but I'm a beginner trying to learn and understand. I came across an exercise to create these three basic functions, and this is what I came up with. 

Any glaring errors? 



This program defines three functions: 


sum(x,y)
range(x,y,step)
rangeSum (start, end)


sum will give you the sum of two numbers.

range will give you an array of numbers from x to y, with an optional step argument that dictates the increment to use when listing the range. If no step is given, the default value is 1. 

rangeSum will give you the sum of a range of numbers from start to end.

// sum(x,y) takes the sum of two numbers
var sum = function(x,y){
  return x + y;
};
// range(x,y,step) gives an array of the range of x and y, with an optional step argument
var range = function(x, y, step){
    if (step == null) step = 1;
  var arr = [];

  if (step &gt; 0) {
    for (var i = x; i &lt;= y; i += step)
      arr.push(i);
  } else {
        for (var i = x; i &gt;= y; i += step)
        arr.push(i);
  }
    return arr;
};
// rangeSum(start,end) gives the sum of the range from start to end.          
var rangeSum = function(start,end){
  var arr = [];
  for (var i = start; i &lt;= end; i++) 
    arr.push(i);
  return arr.reduce(function(a,b){
    return a + b;   
  });
};
The code looks fine but has a few flaws and can be improved. I didn't solve the flaws and change the code: I've left that as an excercise for you.

In general:


Why do you use function expressions instead of function declarations?
I recommend to always use braces, even for single line if statements or for loops. Omitting braces will bite you eventually.
What happens when the parameters of your functions are not numbers or are undefined?


Your code, with my comments added as review points:

// sum(x,y) takes the sum of two numbers
var sum = function(x,y){
    return x + y;
};

// range(x,y,step) gives an array of the range of x and y, with an optional step argument
var range = function(x, y, step){
    if (step == null) step = 1;

    // why do you call this variable arr? 
    // It's better to use a meaningful name like result.
    var arr = [];

    // what happens when y is less than x?
    // what happens when step equals zero?

    if (step &gt; 0) {
        for (var i = x; i &lt;= y; i += step)
            arr.push(i);
    } else {
        for (var i = x; i &gt;= y; i += step)
            arr.push(i);
    }
    return arr;
};

// rangeSum(start,end) gives the sum of the range from start to end. 
// why do you use other argument names than in the range function?
// it's better to use consistent names.
// By the way: the names of these argument are better than the arguments
// of the range function, so I'd change those to start and end.         
var rangeSum = function(start,end){

    // why so complex? You can iterate over the range once and keep a running total.

    var arr = [];
    for (var i = start; i &lt;= end; i++) 
        arr.push(i);
    return arr.reduce(function(a,b){
        return a + b;   
    });
};