# Get a range of numbers

Tell us what’s happening:

The first solution works with the range [4, 1]but not with [5, 10]. Further, I have put a sketch for another solution. Basically, it consist of set a counter and push it each time it increases up to it gets equal to the max number in the original arr.
why my first solution doesn’t work with [5, 10]?
is it good my sketch or do you suggest a different approach?

function sumAll(arr) {
let tempArr = arr.slice();
tempArr.push(absentArrItem(arr))
let secondNum = absentArrItem(arr) - Math.min(...tempArr);
tempArr.push(secondNum);
return tempArr.sort().reduce((a, b) => a + b)
}

let absentArrItem = (arr) => arr.reduce((a, b) => Math.max  (...arr) - Math.min(...arr));

sumAll([4, 1]);

function sumAll(arr) {
//unpack items into 2 vars
//take the smallest and turn it into a counter, which grows and is pushed into a third var up to it gets equal to the highest num of arr (store in the second var)

}

sumAll([10, 5]);

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 OPR/65.0.3467.62.

Challenge: Sum All Numbers in a Range

I’ll try R…

Step 1) copy the original array in tempArr
Step 2) push something more in tempArr
Step 2.2) use absentArrItem to get a number (3)
Step 2.2.1) absentArrItem creates one of the absent numbers based on the original input
Step 3) set the var secondNum to store another number
Step 3.1) the secondNumnumber comes from using absentArrItem and one number from tempArr
Step 4) push secondNum into temArr (actually, I invented this solution based on the idea of using push constantly, because I thought the main thing was create an new array); I also tried to implement functional programming.
Step 5) sort and reduce tempArr(so we get -->10)

Let me try once again:

1. arr is [4, 1]
2. I need to get other 2 numbers from arr
3. First, I need a 3. So, 4 - 1 = 3
4. How do I get it, I wondered? Use Math.max(...arr) - Math.min(...arr) , I thought.

in the exercise you suggest I think the algorithm is:

(I need 3) 4 - 1 = 3
(I need 2) 3 - 1 = 2

But, that doesn’t work with [5, 10]because 10 - 5 = 5, and I don’t need a 5.
My first approach is not useful at all?

Now, the other questions:

The purpose of tempArr is keep the original arr the same,
absetArrItem should create the missing items in the original arrays following that logic,
I realized that the sort thing is no needed, because the result of summing some numbers doesn’t depend on the order

I implemented this:

function sumAll(arr) {
arr.sort((a, b) => a- b)
let counter = arr[0];
let completeArray = [];
while (counter !== arr[1]) {
completeArray.push(counter);
counter++;
}
completeArray.push(arr[1])
return completeArray.reduce((a, b) => a + b)
}

console.log(sumAll([10, 5]))

Thanks for the feedback

Uh, I did this. But I suspect this not what you meant (60 steps):

function sumAll(arr) {
arr.sort((a, b) => a- b)
let counter = arr[0];
let result = 0;
while (counter !== arr[1]) {
counter++;
result = arr.reduce((a, b) => a + b)
arr.push(counter)
}

return result;
}

console.log(sumAll([10, 5]))

Thanks again

function sumAll(arr) {
arr.sort((a, b) => a- b)
let counter = arr[0];
let result = 0;
while (counter <= arr[1]) {
result += counter;
counter++
}

return result;
}

console.log(sumAll([10, 5]))
1 Like