CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

πŸ”˜ Array.prototype.some() Polyfill

The some() method tests if at least one array element passes a test function. Returns true if any element satisfies the condition.

The method tests whether at least one element in the array passes the test implemented by the provided function. -- βœ… Implementation -- How This Works 1. Checks if is a function If not, it throws a (same behavior as ). 2. Iterates through the array Calls for each element with . Uses to bind the optional . 3. Returns early if a match is found If returns , immediately returns . 4. Returns if no match is found If no element passes the test, returns after the loop. -- Edge Cases Handled βœ” Empty array always returns βœ” Works with binding βœ” Stops checking as soon as one match is found (Optimized) βœ” Throws an error if is not a function This version is simple, efficient, and follows the behavior of exactly. -- <!-quiz-start --Q1: What does return? [ ] true [x] false [ ] undefined [ ] Throws an error Q2: When does return ? [ ] When all elements pass the test [x] When at least one element passes the test [ ] When no elements pass the test [ ] When the array is empty Q3: What is the key difference between and ? [ ] some() is faster [x] some() returns true if ANY element passes; every() returns true if ALL pass [ ] every() can handle sparse arrays, some() cannot [ ] some() modifies the array, every() does not <!-quiz-end --
JavaScriptPolyfills
🎯 Array.prototype.at() Polyfill
medium
βœ… Array.prototype.every() Polyfill
medium
πŸ”² Array.prototype.fill() Polyfill
medium
πŸ” Array.prototype.filter() Polyfill
medium
πŸ”Ž Array.prototype.find() Polyfill
medium
πŸ”’ Array.prototype.findIndex() Polyfill
medium
πŸ”™ Array.prototype.findLast() Polyfill
medium
πŸ”™ Array.prototype.findLastIndex() Polyfill
medium
πŸ“‹ Array.prototype.flat() Polyfill
medium
πŸ” Array.prototype.includes() Polyfill
medium
πŸ”’ Array.prototype.indexOf() Polyfill
medium
βœ… Array.isArray() Polyfill
medium
πŸ—ΊοΈ Array.prototype.map() Polyfill
hard
βž– Array.prototype.pop() Polyfill
medium
βž• Array.prototype.push() Polyfill
easy
πŸ”„ Array.prototype.reduce() Polyfill
medium
πŸ”„ Array.prototype.reverse() Polyfill
hard
⬅️ Array.prototype.shift() Polyfill
hard
πŸ”˜ Array.prototype.some() Polyfill
medium
πŸ”€ Array.prototype.sort() Polyfill
hard
➑️ Array.prototype.unshift() Polyfill
hard
πŸ“ž Function.prototype.apply() Polyfill
medium
πŸ”— Function.prototype.bind() Polyfill
hard
πŸ“ž Function.prototype.call() Polyfill
medium
19 of 24
LibraryJavaScriptPolyfills34 of 61

πŸ”˜ Array.prototype.some() Polyfill

jspolyfillsmedium

The some() method tests whether at least one element in the array passes the test implemented by the provided function.


βœ… Implementation

Array.prototype.customSome = function (callback, thisArg) {
    if (typeof callback !== 'function') {
        throw new TypeError(callback + ' is not a function');
    }

    for (let i = 0; i < this.length; i++) {
        if (callback.call(thisArg, this[i], i, this)) {
            return true; // Return true immediately if a match is found
        }
    }

    return false; // Return false if no element satisfies the condition
};

// Example usage:
const array = [1, 2, 3, 4, 5];

const even = (element) => element % 2 === 0;

console.log(array.customSome(even)); // Output: true
console.log(array.customSome((num) => num > 10)); // Output: false

How This Works

  1. Checks if callback is a function
    • If not, it throws a TypeError (same behavior as Array.prototype.some).
  2. Iterates through the array
    • Calls callback for each element with (element, index, array).
    • Uses .call(thisArg, ...) to bind the optional thisArg.
  3. Returns early if a match is found
    • If callback returns true, customSome immediately returns true.
  4. Returns false if no match is found
    • If no element passes the test, customSome returns false after the loop.

Edge Cases Handled

βœ” Empty array always returns false

βœ” Works with thisArg binding

βœ” Stops checking as soon as one match is found (Optimized)

βœ” Throws an error if callback is not a function

This version is simple, efficient, and follows the behavior of Array.prototype.some exactly.


Quick Quiz

Test your understanding with 3 quick questions

Q1What does `[].customSome(x => x > 0)` return?
Q2When does `some()` return `true`?
Q3What is the key difference between `some()` and `every()`?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna