View on GitHub

marymafa.github.io


layout: post title: Drop it date: 2017-11-20 —

Drop it

  1. I were asked to drop the elements of an array (first argument), starting from the front, until the predicate (second argument) returns true.

  2. The second argument, func, is a function you’ll use to test the first elements of the array to decide if you should drop it or not.

  3. Return the rest of the array, otherwise return an empty array.

code explanation

  1. Create a while loop to check each element.

  2. Or create a for loop to check each element.

  3. Then check for the function given if true then stop, otherwise remove that element.

  4. return the array.

My Approach

Firstly, I created a for loop that will iterate though the each arguments. On the test statement i said if the function given is true then stop , otherwise keep on doing until is true. But it was returning inaccurate results then there came a find that why don’t you try while loop because for loop return inaccuarate result.

Finally my func returns true, the loop will break. So in my test case.

my arr[1, 2, 3]

my(simplified)func {return n < 3;}

So myresult should be [1, 2, 3] because 1 < 3 therefore for my while loop is broken and arr is returned.