Sum All Numbers in a Range

function sumAll(arr) {
  // Step 1: Add all numbers greater than x (including x) and all numbers less than y (including y). Example [1,4] = [x,y] = 1 + 2 + 3 + 4 = 10 (Note: the lowest number is not always first)
  return 1;

sumAll([1, 4]);

I wrote a step down of what I believe I have to do, but I am blanking out when it comes to thinking of what code to write to make it happen. Can someone please help me without giving me the answer?

Try splitting it into smaller steps, right now the first step is really the whole task. For example - how to find first number that should be added?

You might also try doing such calculation fully by hand on paper. To some degree each separate action taken would be needed to be made into code.

Thanks for your reply!


So this essentially finds whatever max and min numbers are in the arr?

Ahhhh figured it out. Thank you!

function sumAll(arr) {
let sum = 0; 

for(let i = Math.min.apply(null,arr); i <= Math.max.apply(null,arr); i += 1) {
 sum += i; 
  return sum;

console.log(sumAll([1, 4]));
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.