# 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, arr);
var max = Math.max(arr, arr);
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?

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’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. I think it is somewhere between beginner and normal. The fact that you used reduce in your solution shows you know how to use a more advanced function (reduce) to solve your problem. This tells gives me the impression you are not a beginner. However, using arrays to solve this problem seems more complicated when compared to a solution which using a variable which holds a number representing the running total.

Sometimes my first attempt at a solution tends to be over complicated. For me, I feel I tend to over complicate solutions, when I don’t write out my algorithm in plain language first before attempting to code it. If I just kind of “hack” my way through a solution, I may start off with an approach that is going to end up complicated, because of edge cases I did not think about when I started the solution that particular way. Also, if I don’t get enough sleep, my solutions tend to get over complicated. There are many factors at work here.

Always try your best not to look at a final solution before you have developed your own. Then, always look at other’s solutions and make sure you understand how to simpler and/or more complex solutions actually work. Typically, you will learn something new, which is the whole point.

Congratulate yourself on a working solution you came up with on your own and don’t worry so much if someone else’s “looks better”. At this point, you are still learning how to find the optimal solutions. Sometimes other solution’s appear “better” because they have less characters used, but that does not mean it is a better solution. The best solutions are readable and efficient “enough” for the task at hand.

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, n)) - integerSum(Math.min(n, n) - 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