-4

Guys i'm working through a program to find the largest Number in an negative array.What would be the best algorithm for this. ex: [-72, -3, -17, -10]

rocktimsaikia
  • 137
  • 4
  • 10

2 Answers2

4

You can use Math.max method and spread syntax.

array = [-72, -3, -17, -10]
console.log(Math.max(...array));
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
0

var array = [-72, -3, -17, -10];
var lg = array[0];
var sm = array[0];
for (var i = 1; i < array.length; i++) {
    if (array[i] > lg) {
        lg = array[i];
    }  else if(array[i] < sm) {
        sm = array[i];
    }
}
console.log('largest No :' + lg);
console.log('smallest No :' + sm);
vicky patel
  • 699
  • 2
  • 8
  • 14