JavaScript Array: 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
Updated on: 2020-10-12T11:48:57+05:30

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements