Intermediate Algorithm Scripting: Drop it not understand

How this function working on JavaScript Intermediate Code Challenge. I dnt understand, why 0 not remove from array. Why its not delete 0 on second place when func condition is false. Can anyone explain plz.

function dropIt(arr, func) {
  
  let counter = arr.length;
  for (var i = 0; i < counter; i++) {
    if (func(arr[0])) {
      break;
    } else {
      arr.shift();
    }
  }
  return arr;
}
print(dropIt([0, 1, 0, 1], function(n) {return n === 1;}));

your code works, you changed your function names?..

function dropElements(arr, func) {
  
  let counter = arr.length;
  for (var i = 0; i < counter; i++) {
    if (func(arr[0])) {
      break;
    } else {
      arr.shift();
    }
  }
  return arr;
}
dropElements([0, 1, 0, 1], function(n) {return n === 1;})

yes mate code working but my question is why 0 not remove when its on index 2. simple word why second 0 not remove.

if(func(arr[0])) {
break;
} 

because you have stopped the loop with break when you have found your result so it doesn’t get to index 2

1 Like

So, u mean as soon as primitive Boolean value true loop stop and return array.

yes because the func function returns true or false if the condition is met in this case does n === 1?, so when the if statement gets true value it runs the code which is to stop the loop with break, once this happens the code will move on to the next line of code after the loop which in the case is to return the arr and then the function ends.

1 Like

no condition is false coz 0 !== 1. Thanks anyway I understand now, I’m struggling to understand intermediate algorithms, but its quite fun and interesting topic. really fall in love with algorithms

1 Like