Niklaus Wirth

Niklaus Wirth

Translate

Функция с функцией, как аргумент (31)Drop it

Drop it
31-ый день программы "#100 Days Of Code"

Drop the elements of an array (first argument), starting from the front, until the predicate (second argument) returns true.

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.

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

Функцию можно описать так: Пока есть аргумент в виде массива, то будет выполняться, полученная вторым аргументом, функция, которая добавит в массив только те элементы, которые подойдут под условие заданное полученной функцией.

Here are some helpful links:



function dropElements(arr, func) {
  
  
    while(!func(arr[0])){
       arr.shift();
    }
  
  return arr;
}

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


Ответы:

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

dropElements([0, 1, 0, 1], function(n) {return n === 1;})
return [1, 0, 1].

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

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

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

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


Комментариев нет:

Отправить комментарий