Функциональный JavaScriptМетоды массивовeasy

Функция every

Реализуйте аналог стандартного метода Array#every.

Это задача на работу с функциями. Поэтому every будет не методом массива, а функцией, принимающей два аргумента, первым из которых является массив.

Аргументы

  • array — The array every() was called upon.
  • callback — A function to execute for each element in the array. It should return a truthy value to indicate the element passes the test, and a falsy value otherwise. The function is called with the following arguments:
    • element — The current element being processed in the array.
    • index — The index of the current element being processed in the array.
    • array — The array every() was called upon.

Возвращаемое значение

true unless callbackFn returns a falsy value for an array element, in which case false is immediately returned.

Примеры

console.log(every([1, 2, 3, 4], x => x > 2)); // false console.log(every([1, 2, 3, 4], x => x > 0)); // true console.log(every(["a", "b", "c", "d"], (x, i) => x.length >= i)); // false