Removing Array Elements Using Callback

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will be working on a JavaScript programming challenge that involves manipulating arrays. The goal of this lab is to write a function that removes elements from the end of an array until a specific condition is met, and returns the removed elements. You will be using various JavaScript array methods and a callback function to complete this challenge.


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-28641{{"`Removing Array Elements Using Callback`"}} javascript/data_types -.-> lab-28641{{"`Removing Array Elements Using Callback`"}} javascript/arith_ops -.-> lab-28641{{"`Removing Array Elements Using Callback`"}} javascript/comp_ops -.-> lab-28641{{"`Removing Array Elements Using Callback`"}} javascript/cond_stmts -.-> lab-28641{{"`Removing Array Elements Using Callback`"}} javascript/loops -.-> lab-28641{{"`Removing Array Elements Using Callback`"}} javascript/spread_rest -.-> lab-28641{{"`Removing Array Elements Using Callback`"}} end

Removing Array Elements From the End Until a Condition Is Met

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

Here's a function that removes elements from the end of an array until the passed function returns false. It then returns the removed elements.

To use it, create a reversed copy of the array using the spread operator (...) and Array.prototype.reverse(). Then, loop through the reversed copy using a for...of loop over Array.prototype.entries() until the returned value from the function is falsy.

The callback function, fn, accepts a single argument which is the value of the element. Finally, return the removed elements using Array.prototype.slice().

const takeRightWhile = (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 the function:

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

Summary

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

Other JavaScript Tutorials you may like