Get minimum number without a Math function JavaScript


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

Updated on: 25-Aug-2020

186 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements