Searching for target string in a strangely sorted array in JavaScript


Problem

We are required to write a JavaScript function that takes in a word target and an array of sorted(by length(increasing), number of uppercase letters(decreasing), natural order) unique words which always contains the target.

The task of our function is to find the index(0 based) of the target in the array of words, which would always be in the list.

Example

Following is the code −

 Live Demo

const arr = ['cP', 'rE', 'sZ', 'am', 'bt', 'ev', 'hq', 'rx', 'yi', 'akC', 'nrcVpx', 'iKMVqsj'];
const target = 'akC';
const findTarget = (arr = [], target = '') => {
   const index = arr.indexOf(target);
   return index;
};
console.log(findTarget(arr, target));

Output

Following is the console output −

9

Updated on: 19-Apr-2021

102 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements