FCC Bonfire Series 134: Drop it

I come in with a short one today, similar to the last bonfire, Drop it will have use dealing with arrays and truth tests. This time, we need to drop elements of an array passed in as the first argument, starting from the front, until the truth test (function in the second argument) returns true.

We are going to be using the Array.prototype.shift() method for this task. Similar to pop, which removes the last item in an array, shift removes items from the head and returns the remove item. In other wor.. codes:

var myArray = [1, 2, 3, 4, 5];

var removed = myArray.shift();

console.log(myArray); //-> [2, 3, 4, 5]
console.log(removed); //-> 1

We are going to loop indefinitely, removing the first item from the head if the truth test (also known as predicate) turns out to be false. The first time that an item returns true, we return the array.

Here’s what it looks like:

funtion drop(array, func) {
  while (true) {
    if (array.length === 0 || func(array[0])) {  // If array is empty or truth test passes, return it.
      return array;

    array.shift();  // Remove first item from the head.


That was quick! Notice how we are using array[0] to check the truth test; array[0] will always be the first number in the array, but an item is being removed each time the loop completes the cycle, so it will always be different!

Prepare yourself! Things will start ramping up a bit, so focus, and never falter!