# Can't get passed this one

**URL:** <https://forum.freecodecamp.org/t/cant-get-passed-this-one/442468>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [January 24, 2021, 8:43pm UTC](https://forum.freecodecamp.org/t/cant-get-passed-this-one/442468 "2021-01-24T20:43:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![seferi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/seferi/32/198937_2.png) [@seferi](https://forum.freecodecamp.org/u/seferi)\
**Post date:** [January 24, 2021, 8:43pm UTC](https://forum.freecodecamp.org/t/cant-get-passed-this-one/442468/1 "2021-01-24T20:43:59Z")

</div>

if the number is bigger then the biggest number of the array, test fails..  
Help is appreciated.  
Thanks!!

**Your code so far**

```js

function getIndexToIns(arr, num) {
const sortedArr = arr.sort((a, b) => a - b);

    if (sortedArr.length == 0 || num < sortedArr[0]) {
    return 0; }

for (let i = 0; i < sortedArr.length - 1; i++) {
  if (num == sortedArr[i]) {
    return sortedArr.indexOf(sortedArr[i]);
  }
   if (num > sortedArr[i] && num < sortedArr[i + 1]) {
     return sortedArr.indexOf(sortedArr[i + 1]);
   }
}
return sortedArr[sortedArr.length];
}

getIndexToIns([40, 60], 50);

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0`.

**Challenge:** Where do I Belong

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/where-do-i-belong)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [January 24, 2021, 8:50pm UTC](https://forum.freecodecamp.org/t/cant-get-passed-this-one/442468/2 "2021-01-24T20:50:46Z")

</div>

I don’t understand this line:

```auto
return sortedArr[sortedArr.length];

```

First of all, it’s nonsensical - by definition this will be undefined since the last index in any array is its length - 1. But did you want to return an element from the array? Is that what you wanted?

With a small tweak to this line, I was able to get your code to pass.

---

<div class="post-metadata">

**Author:** ![seferi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/seferi/32/198937_2.png) [@seferi](https://forum.freecodecamp.org/u/seferi)\
**Post date:** [January 24, 2021, 8:58pm UTC](https://forum.freecodecamp.org/t/cant-get-passed-this-one/442468/3 "2021-01-24T20:58:03Z")

</div>

You are absolutely right mate, I forgot to change that line to the index of the last element plus 1 thus changing it to sortedArr.length solved the problem. Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [July 26, 2021, 8:58am UTC](https://forum.freecodecamp.org/t/cant-get-passed-this-one/442468/4 "2021-07-26T08:58:10Z")

</div>

Closed on July 26, 2021, 8:58am UTC

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