# Some question on Intermediate Algorithm Scripting - Drop it

**URL:** <https://forum.freecodecamp.org/t/some-question-on-intermediate-algorithm-scripting-drop-it/569459>\
**Category:** I Built This\
**Created:** [November 7, 2022, 4:02am UTC](https://forum.freecodecamp.org/t/some-question-on-intermediate-algorithm-scripting-drop-it/569459 "2022-11-07T04:02:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Habushu](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/habushu/32/268677_2.png) [@Habushu](https://forum.freecodecamp.org/u/Habushu)\
**Post date:** [November 7, 2022, 4:02am UTC](https://forum.freecodecamp.org/t/some-question-on-intermediate-algorithm-scripting-drop-it/569459/1 "2022-11-07T04:02:50Z")

</div>

**Tell us what’s happening:**  
hello everyone, I would like to ask some questions here. below is the code from the solutions from the hints.

my first question is why do we need to add

```auto
let originalLen = arr.length

```

in the code?

I tried to remove it and use

```auto
for (let i = 0; i < arr.length; i++)

```

instead but it didn’t let me pass one test which is  
`dropElements([1, 2, 3, 4], function(n) {return n > 5;})` should return `[]` .  
could someone please explain why do we need to add let originalLen code and why it won’t pass one of the test if we didn’t add the let originalLen?

second question is  
why is the if function use `arr[0]` instead of `arr[i]` i tried to change the 0 to i and it only pass 2 out of 6 test.  
as far as my understanding arr[0] will only shows the first item in the array while arr[i] will reiterate each item in the array which what we needs.  
what did i miss here?

**Your code so far**

```javascript
function dropElements(arr, func) {
let originalLen = arr.length;
  for (let i = 0; i < originalLen; i++) {
    if (func(arr[0])) {
      break;
    } else {
      arr.shift();
    }
  }
  return arr;
}

console.log(dropElements([1, 2, 3, 4], function(n) {return n >= 3;}));

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36`

**Challenge:** Intermediate Algorithm Scripting - Drop it

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/drop-it)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [November 7, 2022, 4:33am UTC](https://forum.freecodecamp.org/t/some-question-on-intermediate-algorithm-scripting-drop-it/569459/2 "2022-11-07T04:33:11Z")

</div>

This isn’t a great solution. Mutating an array as you iterate like this is messy.

You can tell this is an antipattern because the iteration variable i is never used.

Basically, there are originalLen possible elements that you may need to check.

---

<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:** [May 8, 2023, 4:34pm UTC](https://forum.freecodecamp.org/t/some-question-on-intermediate-algorithm-scripting-drop-it/569459/3 "2023-05-08T16:34:10Z")

</div>

Closed on May 8, 2023, 4:34pm UTC

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