Exploring JavaScript's takeRightUntil Function

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will be exploring the takeRightUntil function in JavaScript. This function removes elements from the end of an array until a given condition is met, and returns the removed elements. We will examine how this function works and how it can be implemented in your code.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced 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/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28640{{"`Exploring JavaScript's takeRightUntil Function`"}} javascript/data_types -.-> lab-28640{{"`Exploring JavaScript's takeRightUntil Function`"}} javascript/arith_ops -.-> lab-28640{{"`Exploring JavaScript's takeRightUntil Function`"}} javascript/comp_ops -.-> lab-28640{{"`Exploring JavaScript's takeRightUntil Function`"}} javascript/cond_stmts -.-> lab-28640{{"`Exploring JavaScript's takeRightUntil Function`"}} javascript/loops -.-> lab-28640{{"`Exploring JavaScript's takeRightUntil Function`"}} javascript/spread_rest -.-> lab-28640{{"`Exploring JavaScript's takeRightUntil Function`"}} end

Removing Array Elements From the End Until a Condition is Met

To start practicing coding, open the Terminal/SSH and type node.

This function removes elements from the end of an array until the passed function returns true and then it returns the removed elements.

Here's how it works:

  • First, create a reversed copy of the array using the spread operator (...) and Array.prototype.reverse().
  • Next, loop through the reversed copy using a for...of loop over Array.prototype.entries() until the returned value from the function is truthy.
  • After that, return the removed elements using Array.prototype.slice().
  • The callback function, fn, accepts a single argument which is the value of the element.

Here's the code:

const takeRightUntil = (arr, fn) => {
  for (const [i, val] of [...arr].reverse().entries())
    if (fn(val)) return i === 0 ? [] : arr.slice(-i);
  return arr;
};

Here's an example of how to use this function:

takeRightUntil([1, 2, 3, 4], (n) => n < 3); // [3, 4]

Summary

Congratulations! You have completed the Remove Array Elements From the End Until Condition Is Met lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like