- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
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
Advertisements
To Continue Learning Please Login
Login with Google