Hone logo
Hone
Problems

Implementing Array.prototype.every in JavaScript

The Array.prototype.every() method is a fundamental tool in JavaScript for checking if all elements in an array satisfy a given condition. Implementing this method yourself is a great exercise in understanding array iteration and functional programming concepts. This challenge asks you to recreate the functionality of every() without using the built-in method.

Problem Description

You are tasked with implementing the Array.prototype.every method. This method takes a callback function as an argument and iterates over each element in the array. The callback function should be invoked with each element, the element's index, and the array itself. every() should return true if the callback function returns true for every element in the array. If the callback returns false for any element, every() should immediately return false.

Key Requirements:

  • The implementation must be added as a method to the Array.prototype.
  • The callback function should adhere to the standard every() signature: callback(element, index, array).
  • If no callback is provided, every() should return true.
  • The implementation should handle empty arrays correctly.

Expected Behavior:

The method should behave identically to the built-in Array.prototype.every() method.

Edge Cases to Consider:

  • Empty Array: Should return true if the array is empty and no callback is provided, or if the callback always returns true.
  • No Callback: Should return true if no callback function is provided.
  • Null/Undefined Callback: Should treat a null or undefined callback as if it always returns true.
  • Non-Array Input: While not strictly required for this exercise, consider how your implementation might handle non-array inputs (e.g., throwing an error or returning a boolean).

Examples

Example 1:

Input: [1, 2, 3, 4, 5].every(x => x > 0)
Output: true
Explanation: All elements in the array are greater than 0.

Example 2:

Input: [1, 2, 3, 4, 5].every(x => x > 2)
Output: false
Explanation: The elements 1 and 2 are not greater than 2.

Example 3:

Input: [].every(x => x > 0)
Output: true
Explanation: The array is empty, so every condition is considered satisfied.

Example 4:

Input: [1, 2, 3, 4, 5].every()
Output: true
Explanation: No callback is provided, so every condition is considered satisfied.

Example 5:

Input: [1, 2, 3, 4, 5].every(x => typeof x === 'number')
Output: true
Explanation: All elements are of type number.

Constraints

  • The implementation must be written in JavaScript.
  • The time complexity should be O(n) in the worst case, where n is the length of the array.
  • The space complexity should be O(1) (constant space).
  • The implementation should not use the built-in Array.prototype.every() method.

Notes

Think about how you can iterate through the array efficiently. Consider using a for loop or a for...of loop. Remember to handle the edge cases carefully to ensure your implementation is robust. The callback function might have unexpected behavior, so be mindful of how it's used.

Loading editor...
javascript