# Where do I belong Challenge Help

**URL:** <https://forum.freecodecamp.org/t/where-do-i-belong-challenge-help/41954>\
**Category:** General\
**Created:** [October 2, 2016, 10:19am UTC](https://forum.freecodecamp.org/t/where-do-i-belong-challenge-help/41954 "2016-10-02T10:19:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![saf94](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/saf94/32/75504_2.png) [@saf94](https://forum.freecodecamp.org/u/saf94)\
**Post date:** [October 2, 2016, 10:19am UTC](https://forum.freecodecamp.org/t/where-do-i-belong-challenge-help/41954/1 "2016-10-02T10:19:44Z")

</div>

Hi there. I can’t figure out where I’m going wrong?? Please help

```
function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  arr = arr.sort(function(a, b){return a-b;});
  
  var answer = 0;
  for (var i=0; i<arr.length; i++) {
    if (num<=arr[i]) {
      answer = arr.indexOf(arr[i]);
    }
  }
  return answer;
}

getIndexToIns([10, 20, 30, 40, 50], 30);
```

---

<div class="post-metadata">

**Author:** ![JohnL3](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/johnl3/32/16459_2.png) [@JohnL3](https://forum.freecodecamp.org/u/JohnL3)\
**Post date:** [October 2, 2016, 10:32am UTC](https://forum.freecodecamp.org/t/where-do-i-belong-challenge-help/41954/2 "2016-10-02T10:32:55Z")

</div>

nearly there … what is happening it is checking all of the numbers in the arr in the for loop and assiging the result of the ones that pass the if to the variable answer but you forgot if say arr[2] meets the criteria it assigns it to answer then goes and checks arr[3] if this is true it now overwrites answer with this .etc etc

delete your return statement  
and change line … answer = arr … to … return arr.indexOf(arr[i])  
now when the if in your for loop finds a num it returns it and dose not continue overwriting the answer.

**tip** put console.log statements in your for loop and watch what happens and this will make things easier as you can see what happens … i generally use repl.it as console.log() statements show up in result screen which makes it handier.

---

<div class="post-metadata">

**Author:** ![saf94](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/saf94/32/75504_2.png) [@saf94](https://forum.freecodecamp.org/u/saf94)\
**Post date:** [October 2, 2016, 10:37am UTC](https://forum.freecodecamp.org/t/where-do-i-belong-challenge-help/41954/3 "2016-10-02T10:37:45Z")

</div>

Yess got it! Thanks 🙂

---

<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, 5:30am UTC](https://forum.freecodecamp.org/t/where-do-i-belong-challenge-help/41954/4 "2021-01-16T05:30:12Z")

</div>

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


