Sorted Array Last Insertion Index

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 insertion index in a sorted array based on a provided iterator function, using JavaScript. The lab will cover how to check if an array is sorted in descending order, how to apply an iterator function to all elements of an array, and how to find the appropriate last index where an element should be inserted, based on the provided iterator function. By the end of the lab, you will have a better understanding of how to manipulate arrays and use iterator functions in JavaScript.


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/array_methods("`Array Methods`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/destr_assign("`Destructuring Assignment`") subgraph Lab Skills javascript/variables -.-> lab-28340{{"`Sorted Array Last Insertion Index`"}} javascript/data_types -.-> lab-28340{{"`Sorted Array Last Insertion Index`"}} javascript/arith_ops -.-> lab-28340{{"`Sorted Array Last Insertion Index`"}} javascript/comp_ops -.-> lab-28340{{"`Sorted Array Last Insertion Index`"}} javascript/array_methods -.-> lab-28340{{"`Sorted Array Last Insertion Index`"}} javascript/higher_funcs -.-> lab-28340{{"`Sorted Array Last Insertion Index`"}} javascript/destr_assign -.-> lab-28340{{"`Sorted Array Last Insertion Index`"}} end

How to Find the Last Insertion Index in a Sorted Array Based on a Function

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

Here's how to find the highest index at which a value should be inserted into an array in order to maintain its sort order, based on a provided iterator function:

  1. Check if the array is sorted in descending order.
  2. Use Array.prototype.map() to apply the iterator function to all elements of the array.
  3. Use Array.prototype.reverse() and Array.prototype.findIndex() to find the appropriate last index where the element should be inserted, based on the provided iterator function.

See the code below:

const sortedLastIndexBy = (arr, n, fn) => {
  const isDescending = fn(arr[0]) > fn(arr[arr.length - 1]);
  const val = fn(n);
  const index = arr
    .map(fn)
    .reverse()
    .findIndex((el) => (isDescending ? val <= el : val >= el));
  return index === -1 ? 0 : arr.length - index;
};

Here's an example:

sortedLastIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, (o) => o.x); // 1

Summary

Congratulations! You have completed the Last Insertion Index in Sorted Array Based on Function lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like