Drop it (Need Help)

function dropElements(arr, func) {

let newArr = []; 
 // Step 1: Create a for loop that goes through everything in the arr
for (let i = 0; i < arr.length; i++){
// Step 2: If the function is not passed, then continue to loop 
  if (arr[i] !== func) {
    i = i + 1; 
  }
    // Step 3: Once the function is passed, return the remaining numbers in the array and remove numbers that didn't pass
    else if(arr[i] == func) {
      arr.shift() == newArr
      return newArr; 
}
  return newArr;
}
}

dropElements([1, 2, 3], function(n) {return n < 3; });

Even though this is wrong, I feel like I am getting better because I was able to get to this without looking at anything. It only passed one of the six tests. Can someone help me out?

In the starter code you can see that the value function(n) {return n < 3; } is passed as the argument for func.

In your if statement condition, you’re checking the equality of numbers against that function. I don’t think the number 3 is ever going to strictly equal a function.

You need to follow this line in the instructions

when the iterated element is passed through it

In other words, you need to call the func function and pass the element from the array, and checking its return value.

2 Likes
function dropElements(arr, func) {

 

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

dropElements([1, 2, 3], function(n) {return n < 3; });

Thanks for the reply! Made adjustments but I am still stuck. I really thought this would work

function dropElements(arr, func) {

 
 // Step 1: Create a for loop that checks each number in arr
for (let i = 0; i < arr.length; i++){
// Step 2: If number plugged into function is true...STOP
  if (func(arr[i])) {
    break; 
  }
  // Step 3: if not true...remove that number
    else {
      arr.shift(); 
  }
}
  return arr;
}

dropElements([1, 2, 3], function(n) {return n < 3; });

This is what I thought I was doing, is my logic off?

Ahhhh I see, do you know a method that removes any number regardless of if its at the beginning, middle, or end? Isn’t it the pop() method

function dropElements(arr, func) {

 
 // Step 1: Create a for loop that checks each number in arr
for (let i = 0; i < arr.length; i++){
// Step 2: If number plugged into function is true...STOP
  if (func(arr[0])) {
    break; 
  }
  // if not true...remove that number
    else {
      arr.shift(); 
  }
}
  return arr;
}

dropElements([1, 2, 3], function(n) {return n < 3; });

It’s working better now that I am specifying the index arr[0], it passes 5/6

Giving answers doesn’t help anyone grow

function dropElements(arr, func) {

let orignalLength = arr.length 
 // Step 1: Create a for loop that checks each number in arr
for (let i = 0; i < orignalLength; i++){
// Step 2: If number plugged into function is true...STOP
  if (func(arr[0])) {
    break; 
  }
  // if not true...remove that number
    else {
      arr.shift(); 
  }
}
  return arr;
}

dropElements([1, 2, 3], function(n) {return n < 3; });

Figured it out, but only because I felt close so I looked at solutions. Somehow let orignalLength = arr.length makes this work. Can you help me understand why? Isn’t creating a variable to name arr.length the same as just using arr.length?

Every time you remove a member of the array, its length becomes one less. This messes with the exit condition logic in your for loop.

The logic becomes much simpler if you avoid mutating an array while looping over it (not only for this problem, in general). For example, you could create a new array that you build up within your loop, and return that at the end of the function.

1 Like

AHHHHH I see, makes perfect sense. Thanks!

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