Sum numbers in a range task

**Tell us what's happening:**
Describe your issue in detail here.
why is my code not passing??

      **Your code so far**
      
```js

function sumAll(arr) {
var newArr = []
var arr1 = Math.min(arr[0], arr[1])
var arr2 = Math.max(arr[0], arr[1])
for (let i = arr[0]; i <= arr[1]; i++) {
newArr.push(i);  
}
return newArr.reduce(function(a, b) {
return a + b;
});
}

console.log(sumAll([1, 4]));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0

Challenge: Sum All Numbers in a Range

Link to the challenge:

Take a look at values you are using in your for loop.

You create arr1 and arr2 to hold the min an max values, but it doesn’t look like they’re being used anywhere.

1 Like

function sumAll(arr) {

  var newArr = []

  var arr1 = Math.min(arr[0], arr[1])

  var arr2 = Math.max(arr[0], arr[1])

  for (let i = arr[0]; i <= arr[1]; i++) {

  newArr.push(i);  

  }

return newArr.reduce(function(a, b) {

return a + b;

});

}

console.log(sumAll([1, 4]))

What happens when you give an argument of [4, 1]?

Your for loop is initialized with the value at arr[0] and stops when it’s more than arr[1].

// arr[0] === 4
// arr[1] === 1
for (let i = 4; i <= 1; i++) {} // this loop wouldn't run
1 Like

should it start initialiazation with the arr[1]??

why did you do this? what was your reasoning? and why do you never use arr1 and arr2?

3 Likes

i did that to get largest number

Why do you need the largest number?

i dont know . i was just following the hint provided on the challlenge

If you console.log(i) in your for loop, what values do you see?

The principle of the challenge is to loop between the smallest and largest values, but you have to know which is larger and which is smaller in order to do that. If you don’t you risk the loop not running at all.

You have arr1 and arr2 as a way to find which is which, but now you’ll need to use these.

Given you’re working with a for loop, were would they fit best?

1 Like

Forget the hints. Forget about arrays.

Do you know how to write a simple for loop that adds together all numbers from 2 to 8?

Once you have that, then you modify your code to put the little number in the input array where 2 is and the big number where 8 is.

1 Like

Ok. Consider @8-bitgaming’s response above with the for loop example. If you want to iterate from one number to another number by counting up (i++), you will have to start with the lower number and count up to the higher number. You obviously can’t count from 7 to 4, for example, by adding 1 to 7 each time. You are using Math.min and Math.max to find the lower number and the higher number. You are assigning variables to those values:

You probably want to use those in your for loop instead of always using the first element of the array as the initial value, and the second as the end. As @8-bitgaming illustrated, the code inside the for loop won’t execute when the initial value is higher than the end value.

2 Likes

Side note, arr1 and arr2 are bad variable names. Those are not arrays.

3 Likes
function sumAll(arr) {
  var newArr = []
  for  (let i = 1; i <= 4; i++) {
  newArr.push(i);  
  }
return newArr.reduce(function(a, b) {
return a + b;
});
}

console.log(sumAll([1, 4]));

Using an array is still a bad way to go about this. I would just use the loop to add instead of making an array and calling reduce


Currently you are not using the function argument. You had code above about finding the min and max of the inputs. Where did that part go?

oh i thought the min and max are irrelevant here.i will have to start allover

In case you missed the instructions

We’ll pass you an array of two numbers. Return the sum of those two numbers plus the sum of all the numbers between them. The lowest number will not always come first.

You must use the two elements in the input array.

finally i crushed it!!! :smiley:

function sumAll(arr) {
  let min  = Math.min(...arr);
  let max = Math.max(...arr);
   let addBetween = 0
  for (let i = min; i <= max; i++) {
    console.log(i)
    addBetween += i
  }
  return addBetween;
}

console.log(sumAll([1, 4]));
2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.