Get minimum number without a Math function JavaScript

JavascriptWeb DevelopmentObject Oriented Programming

We have to write a function that takes in n Number literals as argument, where n is any whole number and returns the smallest number of those numbers without using any library function.

We will solve this problem via a while loop and the code for this will be −

Example

const numbers = [12, 5, 7, 43, -32, -323, 5, 6, 7, 767, 23, 7];
const findMin = (...numbers) => {
   let min = Infinity, len = 0;
   while(len++ < numbers.length){
      min = numbers[len] < min ? numbers[len] : min;
   }
   return min;
};
console.log(findMin(...numbers));

Output

The output in the console will be −

-323
raja
Published on 25-Aug-2020 07:06:26
Advertisements