 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Returning the first number that equals its index in an array using JavaScript
Problem
We are required to write a JavaScript function that takes in an array of number. Our function should return that first number from the array whose value and 0-based index are the same given that there exists at least one such number in the array.
Example
Following is the code −
const arr = [9, 2, 1, 3, 6, 5]; const findFirstSimilar = (arr = []) => {    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(el === i){          return i;       };    }; }; console.log(findFirstSimilar(arr)); Output
3
Advertisements
 