

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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
<h2>Problem</h2><p>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.</p><h2>Example</h2><p>Following is the code −</p><p><a class="demo" href="http://tpcg.io/JWAuGpEp" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notransalte">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));</pre><h2>Output</h2><pre class="result notransalte">3</pre>
- Related Questions & Answers
- Returning the nth even number using JavaScript
- Splitting an array based on its first value - JavaScript
- Finding the index of the first element that violates the series (first non-consecutive number) in JavaScript
- Returning the highest value from an array in JavaScript
- Returning array values that are not odd in JavaScript
- Return the first duplicate number from an array in JavaScript
- Returning only odd number from array in JavaScript
- Finding the first non-consecutive number in an array in JavaScript
- Returning reverse array of integers using JavaScript
- Returning a decimal that have 1s in its binary form only at indices specified by array in JavaScript
- Returning an array in Java
- Returning an array with the minimum and maximum elements JavaScript
- Get the first and last item in an array using JavaScript?
- Returning a range or a number that specifies the square root of a number in JavaScript
- First element that appears even number of times in an array in C++
Advertisements