JavaScript Array showing the index of the lowest number

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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
raja
Published on 12-Oct-2020 11:48:27
Advertisements