Intermediate Algorithm Scripting - Sum All Numbers in a Range

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

I have it starting here because I thought arr[0]+1 would specify the number after the first element in the array, which would be appropriate as I’m trying to find the numbers between the two ascending (hopefully) elements. I thought it would work because the first element is specified by index 0.

But isn’t arr[0] and arr[1] supposed to be part of the sum?

Yes they are part of the sum as well because the question also asks for the sum of the two elements

So why start i at arr[0] + 1?

I think it would be easier if you tell me why I’m wrong

I am telling you.

You just said

So why is arr[0] not part of the numbers you are adding up if you just said you want them to be part of the sum?

return arr[0]+arr[1]+sum its included here

Lets format your code so I can more easily see what is going on

function sumAll(arr) {
  arr.sort();
  let arr2 = [];
  for (let i = arr[0]+1; i < arr[1]; i++) {
    arr2.push(i);
  }
  let sum = 0;
  for (let j = 0; j < arr.length; j++) {
    sum += arr2[j];
  }
  return arr[0] + arr[1] + sum;
}

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

I don’t understand why you did it like that, but I do see those values there.

Why use the array arr2 if you aren’t going to make it cover the whole range you are using? (Though why make arr2 at all?)


It would help if you explain what trouble you are still having.


You have not fixed:

This is a big problem that you need to fix to pass.

When I Google “JavaScript sort array of numbers”, I get very good results, like

function sumAll(arr) {
  arr.sort((a,b)=>a-b)
  let arr2=[]
  for (let i=arr[0]+1;i<arr[1];i++)
arr2.push(i);
let sum=0
for (let j=0;j<arr.length;j++)
sum+=arr2[j]
  return arr[0]+arr[1]+sum

}

console.log(sumAll([10, 5]));

I have updated the sort above.

It says it still does not work for [5,10] or [10,5]

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Applying formatting so other people can read your code:

function sumAll(arr) {
  arr.sort((a, b) => a - b);
  let arr2 = [];
  for (let i = arr[0] + 1; i < arr[1]; i++) {
    arr2.push(i);
  }
  let sum = 0;
  for (let j = 0; j < arr.length; j++) {
    sum += arr2[j];
  }
  return arr[0] + arr[1] + sum;
}

console.log(sumAll([10, 5]));

If you are summing the contents of arr2, your loop bounds need to be related to arr2

boom it worked. I just forgot to put the 2 at the end of arr. Thanks.

Cool.

Now that your code works, I encourage you to think about how you could simplify your code and make it easier for others to understand.

Working code is good, but code that other people can read and understand (or that you can read and understand 6 months from now) is really important.

Yeah tbh I’m still working on the basics for now, but for sure I’ll focus on that in the future. I’m just not sure how to simplify it yet tbh.

The basics include writing clear code.

One way to can simplify your code is to remove arr2 and instead use that loop to create the sum.

function sumAll(arr) {
  arr.sort((a, b) => a - b);
  for (var i = arr[0] + 1; i < arr[1]; i++) {
  }
  let sum = 0
  sum+=i;
  return arr[0] + arr[1] + sum;
}

I don’t understand why this does not work.

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

You need to create the sum before you write the loop to sum things up. Also, there is nothing inside of the loop,