# First algorithm challenge

First algorithm challenge
0

#1

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!

#2

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