Drop it: My code fails the test. If I store arr.length in a variable, it passes the test?

Tell us what’s happening:
Hi, why does the following fail the empty array test?:

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

when the following passes the empty array test:

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

Aren’t they both the same?

   **Your code so far**

function dropElements(arr, func) {

for(let i = 0; i < arr.length; i++){
 if(func(arr[0])){
   break;
 } else {
arr.shift();
 } // close
} // close for
return arr;
 

} // close dropElements

dropElements([1, 2, 3, 4], function(n) {return n > 5;}) should return [].
   **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36

Challenge: Drop it

Link to the challenge:

console.log the array.length and i inside the for loop. And try to figure out whats heppening

Hi, thanks for the reply.

I’ve done this, & I still don’t understand why.

A.
I get 4 from the code, below:

Let origin = arr.length  
console.log(originLen)

B
I get 0, 1, 2, 3, from the following code, which is also a length of 4.

for (let i = 0;  i < arr.length;  i++){
console.log(i)

add here console.log(arr.length) and see what happens when the loop runs

Thanks for the reply. I had already done that before I posted the question.

for (let i = 0;  i < arr.length;  i++) {
console.log(i)
console.log(arr.length)

I got: 0,4,1,4,2,4,3,4

I think you aren’t reading the console log output correctly. It would help to make it clearer. Drop the console.log(i), you don’t need to care about that. Also, add the following at the very beginning of the function (before the for loop):

console.log('arr = ', arr);

The tests are calling your function multiple times, so this will help you see when a new test begins.

okay lemme explain,

this loop will run with respect to the array length.
consider the given example

dropElements([1, 2, 3, 4], function(n) {return n > 5;})

on initial iteration i will be zero and arr.length will be 4.so the condition is true loop will execute.

if is false so else will execute. so the array will become [2,3,4]
As you can see the array is modified and the length of array is no longer 4 its 3. Lets continue the iteration.

now i is 1 and arr.length is 3

again if is false and array will be [3,4]

next iteration i is 2 and arr.length is 2, look the condition for the loop became false even though you havent visited all the array elemnts. There is 2 more elements two left but since the condition become false the loop will quit.

And you returning the arr which is [3,4] but the excepted result is . as you can see the problem is when you mutate the array the length is changing. So the loop wont run as you excepted. But as you said if you store the length in a variable first it will always be the same no matter the array is mutated. Hope that makes sense

  1. I think you should start by asking yourself what is the difference between arr.length and the length saved to a variable? What part of your code changes one but not the other?

  2. Does your code ever hit the break statement? If not why does the loop end?

  3. When does a for loop increment the index?

function dropElements(arr, func) {
  let index = 0;
  for (index = 0; index < arr.length; index++) {
    console.log('length before shift', arr.length);
    if (func(arr[0])) {
      console.log('Did I run?');
      break;
    } else {
      arr.shift();
      console.log('length after shift', arr.length);
    }
  }
  console.log('index after loop', index);
  return arr;
}

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

/*
length before shift 4
length after shift 3
length before shift 3
length after shift 2
index after loop 2
[ 3, 4 ]
*/

Last I would consider if there isn’t a different loop construct that better fits your code. Maybe something that loops as long as arr.length is a truthy value and doesn’t care about the index (considering you aren’t using the index for anything other than the loop condition).

Thanks for explaining. That makes sense. Have a good week.

1 Like

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