Search⌘ K

Solution Review: Where to Insert

Explore how to convert traditional functions into arrow functions and apply the array reduce method to solve problems efficiently. Understand the use of accumulator and current value arguments in reducers and how to implement concise logic for JavaScript coding interviews.

We'll cover the following...

There are various ways to solve the problem provided in the previous lesson. However, the question asks for an efficient solution using arrow functions.

Solution #

Let’s look at the fast-way of solving the problem using arrow functions.

Javascript (babel-node)
const getIndex = (arr, number) =>
arr.reduce((counter, curr) => (number > curr ? ++counter : counter), 0);
var inputsA = [[10,5,1,3],[16,4,5]];
var inputsB = [2,13]
console.log(getIndex(inputsA[0],inputsB[0]))

Explanation #

The first part of the question requires you ...