# Problem with return algorithm scripting

**URL:** <https://forum.freecodecamp.org/t/problem-with-return-algorithm-scripting/332545>\
**Category:** General\
**Created:** [December 11, 2019, 8:11pm UTC](https://forum.freecodecamp.org/t/problem-with-return-algorithm-scripting/332545 "2019-12-11T20:11:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Floki](https://avatars.discourse-cdn.com/v4/letter/f/34f0e0/32.png) [@Floki](https://forum.freecodecamp.org/u/Floki)\
**Post date:** [December 11, 2019, 8:11pm UTC](https://forum.freecodecamp.org/t/problem-with-return-algorithm-scripting/332545/1 "2019-12-11T20:11:37Z")

</div>

Hey guys,

I “solved” the first challenge but i can pass the test of freeCodeCamp.

**Code**

```auto
var myArray = [5, 6, 10];
var max = 0;
var min;
var result = 0;

function sumAll(arr) {
  // max of array
  for (let i = 0; i < arr.length; i++) {
    if (myArray[i] > max) {
      max = myArray[i];
    }
  }
  min = max;
  console.log(max);
  // min of array
  for (let i = 0; i < arr.length; i++) {
    if (myArray[i] < min) {
      min = myArray[i];
    }
  }
  console.log(min);
  //calc result
  for (let i = min; i <= max; i++) {
    result += i;
  }
  console.log(result);
}
sumAll([1, 4, 2]);

```

My problem is i have to return the “result” but if I use return my function get immediately stopped. I don´t know how to use the return statement at this point.

cheers  
Flo

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [December 11, 2019, 8:23pm UTC](https://forum.freecodecamp.org/t/problem-with-return-algorithm-scripting/332545/2 "2019-12-11T20:23:49Z")

</div>

> [@Floki](#):
>
> My problem is i have to return the “result” but if I use return my function get immediately stopped.

post your code with the return statement- where are you putting it?  
you are right that a return statement stops the function, but if it is stopping the function at the wrong time then it is at the wrong place

---

<div class="post-metadata">

**Author:** ![Floki](https://avatars.discourse-cdn.com/v4/letter/f/34f0e0/32.png) [@Floki](https://forum.freecodecamp.org/u/Floki)\
**Post date:** [December 11, 2019, 8:29pm UTC](https://forum.freecodecamp.org/t/problem-with-return-algorithm-scripting/332545/3 "2019-12-11T20:29:07Z")

</div>

> [@Floki](#):
>
> lt)

Sorry I missed that…

```auto
var myArray = [1, 4, 2];
var max = 0;
var min;
var result = 0;

function sumAll(arr) {
  // max of array
  for (let i = 0; i < arr.length; i++) {
    if (myArray[i] > max) {
      max = myArray[i];
    }
  }
  min = max;
  console.log(max);
  // min of array
  for (let i = 0; i < arr.length; i++) {
    if (myArray[i] < min) {
      min = myArray[i];
    }
  }
  console.log(min);
  //calc result
  for (let i = min; i <= max; i++) {
    result += i;
  }
  return result;
}
sumAll([1, 4,]);

```

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [December 11, 2019, 8:31pm UTC](https://forum.freecodecamp.org/t/problem-with-return-algorithm-scripting/332545/4 "2019-12-11T20:31:55Z")

</div>

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

```javascript
var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

```

Additionally, I’m confused by your `myArray` global variable. Where does that come from?

---

<div class="post-metadata">

**Author:** ![Floki](https://avatars.discourse-cdn.com/v4/letter/f/34f0e0/32.png) [@Floki](https://forum.freecodecamp.org/u/Floki)\
**Post date:** [December 11, 2019, 8:54pm UTC](https://forum.freecodecamp.org/t/problem-with-return-algorithm-scripting/332545/5 "2019-12-11T20:54:13Z")

</div>

So, I finally got it thanks for the explanation 🙂

I did a lot of testing and forgot to delete the array ^^ My bad.

here is the final code:

```auto
function sumAll(arr) {
  var max = 0;
  var min;
  // max of array
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] > max) {
      max = arr[i];
    }
  }
  min = max;
  console.log(max);
  // min of array
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] < min) {
      min = arr[i];
    }
  }
  console.log(min);
  //calc result
   var result = 0;
  for (let i = min; i <= max; i++) {
   
    result += i;
  }
  return result;
}
sumAll([1, 4,]);

```

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [December 11, 2019, 8:56pm UTC](https://forum.freecodecamp.org/t/problem-with-return-algorithm-scripting/332545/6 "2019-12-11T20:56:09Z")

</div>

Congratulations on figuring it out. Happy coding!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 11:30am UTC](https://forum.freecodecamp.org/t/problem-with-return-algorithm-scripting/332545/7 "2021-01-16T11:30:40Z")

</div>

Closed on January 16, 2021, 11:30am UTC


