Overcomplicating?

Hi there,

I just have a general question about coding in Javascript.

Sometimes I think I overcomplicate things.
For example, my solution to “Sum All Numbers in a Range” is:

function sumAll(arr) {
 var newArr = [];
 var min = Math.min(arr[0], arr[1]);
 var max = Math.max(arr[0], arr[1]);
 var numbers = max - min;

 newArr.push(min);
  
 for (i=1; i<numbers; i++)
 {
   newArr.push(min+i);
 }
 
 newArr.push(max);
  
 var sum = newArr.reduce(function (a, b) 
 {
  return a + b;
 }, 0);
  
 return sum;
}

I got this solution in 5-10 minutes, but when I see the other solutions,
I felt that mine is overcomplicated.
Is this normal at beginner level?

Thank you for your answers!

1 Like

I would say that yes, it’s normal at the beginner level. (Mind you, I still consider myself a beginner)

I think the important thing is that you work through the problem logically and figure out a creative way of getting the computer to do what you want. More efficient solutions will come up as you become more familiar with the various things you can do with your code.

You can also study some other people’s solutions to learn more about how you can improve your own, and then apply what you learn from that comparison to your future projects.

At the beginner level, you will almost always be able to find some code online somewhere that does what you are being asked to do. However, if you simply copy that code because it is a “better” or “less complicated” solution than anything you can come up with, then you will not be training yourself to think like a programmer. It’s always important to try to figure out your own solution first.

1 Like

I don’t see anything wrong with it. It’s very clear what is happening. Some solutions sacrifice clarity for concision. That’s not always a good thing.

1 Like

Thank you guys for your replies!
I hope that this “overcomplication-effect” will be gone
with more experience and confidence in webdev.

Have a nice day!

p.s.: rmdawson71 : thank you for the readability editing!

A simple way to avoid overcomplicating mathematical operations is to break them
down in the same way we would a mathematical formula.

First let’s look at summing integers.

The sum of integers from 1 to n can be computed with the formula n*(n+1)/2.

Now order to compute the sum of integers between (inclusive) two integers,
we can simply take the integer sum of the larger integer and subtract the
integer sum of 1 less than the smaller integer.

function integerSum(n) {
    return (n * (n + 1)) / 2;
}

function sumAll(a, b) {
    return integerSum(Math.max(a, b)) - integerSum(Math.min(a, b) - 1);
}

Breaking the solution down algebraicly allowed us to write two simpe functions
that did not require creation of any variables to provide the resulting value.

You COULD rewrite the function to perform the operation integerSum inside of
sumAll but you would either want to cast to a variable or call Math.max and
Math.min twice. I haven’t ran performance tests of the different variations
to say which might be better, but the way I’ve provided is rather efficient.

Your original example code used an array of 2 integers as it’s input, which we
can reproduce by adjusting our sumAll slightly as follows:

function sumAll(n) {
    return integerSum(Math.max(n[0], n[1])) - integerSum(Math.min(n[0], n[1]) - 1);
}

These same functions can be expressed in ES6 syntax as follows:

const integerSum = n => (n * (n + 1)) / 2;

const sumAll = (a, b) => integerSum(Math.max(a, b)) - integerSum(Math.min(a, b) - 1);

Or to allow for an arbitrary number of possible provided arguments:

const integerSum = n => (n * (n + 1)) / 2;

const sumAll = (...n) => integerSum(Math.max(...n)) - integerSum(Math.min(...n) - 1);

Or using your original function signature:

const integerSum = n => (n * (n + 1)) / 2;

const sumAll = (n) => integerSum(Math.max(...n)) - integerSum(Math.min(...n) - 1);

Added jspref test and as expected the initial 2 function solution is pretty efficient (sumAll0 in the tests), factoring out to a single function with variables is slightly faster (sumAll2), and running multiple Math.max or Math.min calls was slower (sumAll1).

Justin, the point is to learn how to code algorithms, not how to find shortcut formulas. Yes, sometimes there is an easier way to find an answer, but there weren’t always. The point of the section “Intermediate Algorithm Scripting” is to build and test algorithmic skills. There won’t always be a shortcut formula.

1 Like