Sum of integers in an interval Here is my solution to a series of problems in JS fundamentals. I've made it pass but I know this could be more efficient. I'm not looking for anyone to rewrite the code, but just asking what methods should be used when writing similar code in the future.
The goal is basically to input two parameters, both have to be positive numbers and typeof numbers otherwise return ERROR. Also form an array that adds said two integers and all numbers between them. For example: if you enter 1,4 the answer would compute 1 + 2 + 3 + 4 = 10, and return 10.
const sumAll = function(lowEnd, highEnd) {
    let total = [];
    let sum = 0;
    let first = lowEnd;
    let second = highEnd;
    if (typeof first === 'number' &amp;&amp; typeof second === 'number') {
        if (lowEnd &gt;= 0 &amp;&amp; highEnd &gt;= 0) {    
            if (lowEnd &lt;= highEnd){
                for (let i = lowEnd; i &lt;= highEnd; i++) {
                    total.push(i);  
                }
            } 
            else {
                for (let i = highEnd; i &lt;= lowEnd; i++) {
                    total.push(i);  
                }
            }
            for (let i = 0; i &lt; total.length; i++) {
                sum += total[i];
            };
            return sum;
        } 
        else if (lowEnd &lt; 0 || highEnd &lt; 0)
            return 'ERROR';
    } 
    else {
        return 'ERROR';
    }
}

and here are the tests,
const sumAll = require('./sumAll')

describe('sumAll', function() {
  it('sums numbers within the range', function() {
    expect(sumAll(1, 4)).toEqual(10);
  });
  it('works with large numbers', function() {
    expect(sumAll(1, 4000)).toEqual(8002000);
  });
  it('works with larger number first', function() {
    expect(sumAll(123, 1)).toEqual(7626);
  });
  it('returns ERROR with negative numbers', function() {
    expect(sumAll(-10, 4)).toEqual('ERROR');
  });
  it('returns ERROR with non-number parameters', function() {
    expect(sumAll(10, &quot;90&quot;)).toEqual('ERROR');
  });
  it('returns ERROR with non-number parameters', function() {
    expect(sumAll(10, [90, 1])).toEqual('ERROR');
  });
});
A good addition is to add a check at the start of your function to check if your parameters are correct. This way you don't need extra if statements which helps for readability.


const isValidNum = (n) =&gt; typeof n === "number" &amp;&amp; n &gt;= 0;

const sumAll = function(lowEnd, highEnd) {
  if (!isValidNum(lowEnd) || !isValidNum(highEnd)) return "ERROR";

  let total = [];
  let sum = 0;

  if (lowEnd &lt;= highEnd) {
    for (let i = lowEnd; i &lt;= highEnd; i++) {
      total.push(i);
    }
  } else {
    for (let i = highEnd; i &lt;= lowEnd; i++) {
      total.push(i);
    }
  }

  for (let i = 0; i &lt; total.length; i++) {
    sum += total[i];
  };

  return sum;
}

console.log(sumAll(1, 4));
console.log(sumAll(4, 1));
console.log(sumAll("1", 4));
console.log(sumAll(1, "4"));
console.log(sumAll(1, -4));
console.log(sumAll(-1, -4));



Another option is to check for the highest and lowest number via Math.min() and Math.max(). This way the order of input doesn't matter and you don't have to write the loop twice.


const isValidNum = (n) =&gt; typeof n === "number" &amp;&amp; n &gt;= 0;

const sumAll = function(lowEnd, highEnd) {
  if (!isValidNum(lowEnd) || !isValidNum(highEnd)) return "ERROR";

  let total = [];
  let sum = 0;
  
  const lowest = Math.min(lowEnd, highEnd);
  const highest = Math.max(lowEnd, highEnd);

  for (let i = lowest; i &lt;= highest; i++) {
    total.push(i);
  }

  for (let i = 0; i &lt; total.length; i++) {
    sum += total[i];
  };

  return sum;
}

console.log(sumAll(1, 4));
console.log(sumAll(4, 1));
console.log(sumAll("1", 4));
console.log(sumAll(1, "4"));
console.log(sumAll(1, -4));
console.log(sumAll(-1, -4));



At last you could add a formula to calculate the total sum of a range of numbers. Which removes the need of loops. (More info about the formula here)


const isValidNum = (n) =&gt; typeof n === "number" &amp;&amp; n &gt;= 0;

const sumRange = (low, high) =&gt; ((high - low + 1) * (low + high)) / 2;

const sumAll = function(lowEnd, highEnd) {
  if (!isValidNum(lowEnd) || !isValidNum(highEnd)) return "ERROR";
  
  const lowest = Math.min(lowEnd, highEnd);
  const highest = Math.max(lowEnd, highEnd);

  return sumRange(lowest, highest);
}

console.log(sumAll(1, 4));
console.log(sumAll(4, 1));
console.log(sumAll("1", 4));
console.log(sumAll(1, "4"));
console.log(sumAll(1, -4));
console.log(sumAll(-1, -4));



Total revision with comments:


// Return true if "n" is positive and of type number, else return false
const isValidNum = (n) =&gt; typeof n === "number" &amp;&amp; n &gt;= 0;

// Formula for adding a range of numbers
const sumRange = (low, high) =&gt; ((high - low + 1) * (low + high)) / 2;

function sumAll(lowEnd, highEnd) {
  // Return "ERROR" if arguments are invalid
  if (!isValidNum(lowEnd) || !isValidNum(highEnd)) return "ERROR";

  // Find highest and lowest number
  const lowest = Math.min(lowEnd, highEnd);
  const highest = Math.max(lowEnd, highEnd);

  // Return added numbers
  return sumRange(lowest, highest);
}

console.log(sumAll(1, 4));
console.log(sumAll(4, 1));
console.log(sumAll("1", 4));
console.log(sumAll(1, "4"));
console.log(sumAll(1, -4));
console.log(sumAll(-1, -4));



PS: I really like the way your tests are written. nicely clean and concise.