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.findLast() Polyfill

The findLast() method returns the last element satisfying a test function, iterating from end to start. Reverses find() behavior.

The method returns the last element in an array that satisfies a given predicate function. It iterates from the end of the array. -- βœ… Implementation Explanation: 1. Check if is defined: The condition ensures that the polyfill only runs if is not already available. 2. Callback validation: It checks whether the provided is a valid function using . 3. Iterate backward: The loop starts from the last element of the array () and moves towards the first element (). 4. Calling the callback: is used to invoke the function. This gives you the correct value for within the callback function, the current element (), its index (), and the array itself (). 5. Return the matching element: If the callback returns a truthy value, that element is returned as the result of . 6. Return if no match is found: If the loop completes without finding a match, is returned. Example Usage: In this example, finds the last even number (4) in the array. Edge Cases: If no element matches the condition, it returns . If the array is empty, it returns . This implementation closely mimics the behavior of the modern method that was introduced in newer versions of JavaScript. -- <!-quiz-start --Q1: What does return? [ ] 2 [x] 4 [ ] [2, 4] [ ] 3 Q2: How does differ from ? [ ] findLast() returns all matching elements [ ] findLast() is slower [x] findLast() iterates from the end of the array [ ] findLast() returns the index instead of the element Q3: What does return when no element matches? [ ] -1 [ ] null [ ] The last element of the array [x] undefined <!-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
7 of 24
LibraryJavaScriptPolyfills22 of 61

πŸ”™ Array.prototype.findLast() Polyfill

jspolyfillsmedium

The findLast() method returns the last element in an array that satisfies a given predicate function. It iterates from the end of the array.


βœ… Implementation

if (!Array.prototype.findLast) {
  Array.prototype.findLast = function(callback, thisArg) {
    // Ensure that 'callback' is a function
    if (typeof callback !== 'function') {
      throw new TypeError(callback + ' is not a function');
    }
    
    // Iterate over the array starting from the last element
    for (let i = this.length - 1; i >= 0; i--) {
      // If the element satisfies the condition, return it
      if (callback.call(thisArg, this[i], i, this)) {
        return this[i];
      }
    }
    
    // If no element satisfies the condition, return undefined
    return undefined;
  };
}

Explanation:

  1. Check if findLast is defined: The if (!Array.prototype.findLast) condition ensures that the polyfill only runs if findLast is not already available.

  2. Callback validation: It checks whether the callback provided is a valid function using typeof callback !== 'function'.

  3. Iterate backward: The loop starts from the last element of the array (i = this.length - 1) and moves towards the first element (i--).

  4. Calling the callback: callback.call(thisArg, this[i], i, this) is used to invoke the callback function. This gives you the correct value for this within the callback function, the current element (this[i]), its index (i), and the array itself (this).

  5. Return the matching element: If the callback returns a truthy value, that element is returned as the result of findLast.

  6. Return undefined if no match is found: If the loop completes without finding a match, undefined is returned.

Example Usage:

const arr = [1, 2, 3, 4, 5];

const result = arr.findLast(num => num % 2 === 0);
console.log(result); // Output: 4

In this example, findLast finds the last even number (4) in the array.

Edge Cases:

  • If no element matches the condition, it returns undefined.
  • If the array is empty, it returns undefined.

This implementation closely mimics the behavior of the modern findLast method that was introduced in newer versions of JavaScript.


Quick Quiz

Test your understanding with 3 quick questions

Q1What does `[1, 2, 3, 4, 5].findLast(num => num % 2 === 0)` return?
Q2How does `findLast()` differ from `find()`?
Q3What does `findLast()` return when no element matches?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna