Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
JavaScript Array showing the index of the lowest number
We are required to write a JavaScript function that takes in an array of numbers. Then the function should return the index of the smallest number in the array.
Example
The code for this will be −
const arr = [3, 56, 56, 23, 7, 76, -2, 345, 45, 76, 3];
const lowestIndex = arr => {
const creds = arr.reduce((acc, val, ind) => {
let { num, index } = acc;
if(val < num){
num = val;
index = ind;
};
return { num, index };
}, {
num: Infinity,
index: -1
});
return creds.index;
};
console.log(lowestIndex(arr));
Output
The output in the console −
6
Advertisements