Skip to main content
corrected since this is not fastest,according to multiple users tests. (no offense!)
Source Link
ashleedawg
  • 21.9k
  • 9
  • 82
  • 120

The fastestOne way, in this case, is loopingto loop through all elements, and compare it to the highest/lowest value, so far.

(Creating an array, invoking array methods is overkill for this simple operation).

 // There's no real number bigger than plus Infinity var lowest = Number.POSITIVE_INFINITY; var highest = Number.NEGATIVE_INFINITY; var tmp; for (var i=myArray.length-1; i>=0; i--) { tmp = myArray[i].Cost; if (tmp < lowest) lowest = tmp; if (tmp > highest) highest = tmp; } console.log(highest, lowest); 

The fastest way, in this case, is looping through all elements, and compare it to the highest/lowest value, so far.

(Creating an array, invoking array methods is overkill for this simple operation).

 // There's no real number bigger than plus Infinity var lowest = Number.POSITIVE_INFINITY; var highest = Number.NEGATIVE_INFINITY; var tmp; for (var i=myArray.length-1; i>=0; i--) { tmp = myArray[i].Cost; if (tmp < lowest) lowest = tmp; if (tmp > highest) highest = tmp; } console.log(highest, lowest); 

One way is to loop through all elements and compare it to the highest/lowest value.

(Creating an array, invoking array methods is overkill for this simple operation).

 // There's no real number bigger than plus Infinity var lowest = Number.POSITIVE_INFINITY; var highest = Number.NEGATIVE_INFINITY; var tmp; for (var i=myArray.length-1; i>=0; i--) { tmp = myArray[i].Cost; if (tmp < lowest) lowest = tmp; if (tmp > highest) highest = tmp; } console.log(highest, lowest); 
Removed double declaration
Source Link
Rob W
  • 350.5k
  • 87
  • 811
  • 683

The fastest way, in this case, is looping through all elements, and compare it to the highest/lowest value, so far.

(Creating an array, invoking array methods is overkill for this simple operation).

 // There's no real number bigger than plus Infinity var lowest = Number.POSITIVE_INFINITY; var highest = Number.NEGATIVE_INFINITY; var tmp; for (var i=myArray.length-1; i>=0; i--) { var tmp = myArray[i].Cost; if (tmp < lowest) lowest = tmp; if (tmp > highest) highest = tmp; } console.log(highest, lowest); 

The fastest way, in this case, is looping through all elements, and compare it to the highest/lowest value, so far.

(Creating an array, invoking array methods is overkill for this simple operation).

 // There's no real number bigger than plus Infinity var lowest = Number.POSITIVE_INFINITY; var highest = Number.NEGATIVE_INFINITY; var tmp; for (var i=myArray.length-1; i>=0; i--) { var tmp = myArray[i].Cost; if (tmp < lowest) lowest = tmp; if (tmp > highest) highest = tmp; } console.log(highest, lowest); 

The fastest way, in this case, is looping through all elements, and compare it to the highest/lowest value, so far.

(Creating an array, invoking array methods is overkill for this simple operation).

 // There's no real number bigger than plus Infinity var lowest = Number.POSITIVE_INFINITY; var highest = Number.NEGATIVE_INFINITY; var tmp; for (var i=myArray.length-1; i>=0; i--) { tmp = myArray[i].Cost; if (tmp < lowest) lowest = tmp; if (tmp > highest) highest = tmp; } console.log(highest, lowest); 
added 47 characters in body
Source Link
Rob W
  • 350.5k
  • 87
  • 811
  • 683

The fastest way, in this case, is looping through all elements, and compare it to the highest/lowest value, so far.

(Creating an array, invoking array methods is overkill for this simple operation).

 // There's no real number bigger than plus Infinity var lowest = Number.POSITIVE_INFINITY; // Or: Infinity var (-Infinityhighest for= NEGATIVE)Number.NEGATIVE_INFINITY; var tmp; for (var i=myArray.length-1; i>=0; i--) { var tmp = myArray[i].Cost; //if Replace(tmp < with > for gettinglowest) thelowest highest= numbertmp; if (tmp <> lowesthighest) lowesthighest = tmp; } console.log(highest, lowest); 

The fastest way, in this case, is looping through all elements, and compare it to the highest/lowest value, so far.

(Creating an array, invoking array methods is overkill for this simple operation).

 // There's no real number bigger than plus Infinity var lowest = Number.POSITIVE_INFINITY; // Or: Infinity (-Infinity for NEGATIVE) var tmp; for (var i=myArray.length-1; i>=0; i--) { var tmp = myArray[i].Cost; // Replace < with > for getting the highest number if (tmp < lowest) lowest = tmp; } 

The fastest way, in this case, is looping through all elements, and compare it to the highest/lowest value, so far.

(Creating an array, invoking array methods is overkill for this simple operation).

 // There's no real number bigger than plus Infinity var lowest = Number.POSITIVE_INFINITY; var highest = Number.NEGATIVE_INFINITY; var tmp; for (var i=myArray.length-1; i>=0; i--) { var tmp = myArray[i].Cost; if (tmp < lowest) lowest = tmp; if (tmp > highest) highest = tmp; } console.log(highest, lowest); 
Source Link
Rob W
  • 350.5k
  • 87
  • 811
  • 683
Loading