# First algorithm challenge

**URL:** <https://forum.freecodecamp.org/t/first-algorithm-challenge/25192>\
**Category:** General\
**Created:** [August 3, 2016, 9:08pm UTC](https://forum.freecodecamp.org/t/first-algorithm-challenge/25192 "2016-08-03T21:08:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MN4PVK4](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mn4pvk4/32/12109_2.png) [@MN4PVK4](https://forum.freecodecamp.org/u/MN4PVK4)\
**Post date:** [August 3, 2016, 9:08pm UTC](https://forum.freecodecamp.org/t/first-algorithm-challenge/25192/1 "2016-08-03T21:08:47Z")

</div>

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

for (var i = small; i\<arr.length;i++){  
if(i \< big){  
arr.push(i);

}  
}  
for (var j = big; i\<arr.length;i++){  
if(j \> small){  
arr.push(j);

}  
}

for (var n = small; n\<arr.length; n++){  
if(n !== arr[i]) {  
newArr.push(n);  
}  
}  
return newArr.reduce(function(a, b){  
return a+b;  
}  
);

}

sumAll([1, 4]);

So, with this function, I am getting what I am supposed to with 1, 4.  
and 4, 1. When I try to plug in 5, 10, and 10, 5, though, it doesnt  
even run the for loop.

In other words I don’t think it’s running the for loop unless “small” is one. If it is bigger it won’t seem to compute!

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [August 3, 2016, 9:29pm UTC](https://forum.freecodecamp.org/t/first-algorithm-challenge/25192/2 "2016-08-03T21:29:49Z")

</div>

I’m copying my replay to your other thread here (with slight edits).

You’re very close. In fact, you’ve already written too much. Think about the first loop

```
for (var i = small; i < arr.length; i++) {
        if (i < big) {
            arr.push(i);
    }
}

```

I believe your idea is to start the loop at the lower number, then add all the numbers between `small` and `big`, exclusive. I assume that because it makes sense, but there’s a problem. For an array of size 2, this will only run twice. When `small` and `big` only have two numbers in between them, like `[1,4]`, that works fine. When the numbers are farther apart, like `[5,10]`, it adds two numbers and then quits out. Your code _almost_ works, though. Here are some hints:

- Freebie - get rid of the `if` clause in your loop.
- Think about why you had the `if` clause there in the first place. It’s not a bad idea, but where in the loop declaration would it be more useful?
- You’ll have to make one more _slight_ change to the loop declaration to get it to work. What’s the first number your loop will add to the array?
- Do the other two `for` loops do anything for you? What will the outcome of the first loop be? **strong text**

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [January 16, 2021, 4:40am UTC](https://forum.freecodecamp.org/t/first-algorithm-challenge/25192/3 "2021-01-16T04:40:56Z")

</div>

Closed on January 16, 2021, 4:40am UTC


