Find Last N Matches

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the concept of finding the last n elements that satisfy a given condition in an array. We will learn how to use a for loop to iterate through the array and execute a provided function on each element. We will also learn how to use the Array.prototype.unshift() method to prepend matching elements to the results array and return it once the desired length is reached.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") javascript/BasicConceptsGroup -.-> javascript/loops("`Loops`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") javascript/BasicConceptsGroup -.-> javascript/obj_manip("`Object Manipulation`") subgraph Lab Skills javascript/variables -.-> lab-28307{{"`Find Last N Matches`"}} javascript/data_types -.-> lab-28307{{"`Find Last N Matches`"}} javascript/arith_ops -.-> lab-28307{{"`Find Last N Matches`"}} javascript/comp_ops -.-> lab-28307{{"`Find Last N Matches`"}} javascript/cond_stmts -.-> lab-28307{{"`Find Last N Matches`"}} javascript/loops -.-> lab-28307{{"`Find Last N Matches`"}} javascript/array_methods -.-> lab-28307{{"`Find Last N Matches`"}} javascript/obj_manip -.-> lab-28307{{"`Find Last N Matches`"}} end

Instructions to find Last N Matches

To find the last n elements that match a certain condition, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use the findLastN function provided below.
  3. Provide an array arr and a matcher function that returns a truthy value for the elements you want to match.
  4. Optionally, you can also provide the number n of matches you want to return (default is 1).
  5. The function will execute the matcher function for each element of arr using a for loop, starting from the last element.
  6. If an element matches the matcher condition, it will be added to the results array using Array.prototype.unshift(), which prepends elements to the array.
  7. When the length of the results array is equal to n, the function will return the results.
  8. If there are no matches or n is greater than the number of matches, an empty array will be returned.
const findLastN = (arr, matcher, n = 1) => {
  let res = [];
  for (let i = arr.length - 1; i >= 0; i--) {
    const el = arr[i];
    const match = matcher(el, i, arr);
    if (match) res.unshift(el);
    if (res.length === n) return res;
  }
  return res;
};

Here are some examples of how to use the findLastN function:

findLastN([1, 2, 4, 6], (n) => n % 2 === 0, 2); // [4, 6]
findLastN([1, 2, 4, 6], (n) => n % 2 === 0, 5); // [2, 4, 6]

Summary

Congratulations! You have completed the Find Last N Matches lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like