Seniors checkout my code and give reviews

function sumAll(arr) {

    arr.sort((x,y)=>{

        return x-y;

    });

    let x = arr[0];  // 1

    let y = arr[1]; //4

      

    for(let i = x+1 ; i < y ; i++){

      arr.push(i);

    } 

    let answer = arr.reduce((acc,x)=>{

      return acc + x;

    });

   return answer;

}

sumAll([10, 5]);

Review it for what? What are you trying to do? Is it working? Is there some part you think can be improved? Is there some part you are confused about?


I’ve edited your post 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 would at least use better variable names than x and y.

This function appears to be summing all of the integers between two numbers (including the two numbers passed in). It seems like you are going to a lot of extra work just to add consecutive integers (creating a new array and then sorting it and reducing it). Seems to me that you should be able to do all of this with just a simple for loop.

Also, I think there is a simple mathematical formula you can use that wouldn’t even require a for loop. I’m no math genius so I can’t tell you what it is off the top of my head but I’m pretty sure I remember reading about it.

1 Like

I found the challenge link so now people will have some context.