Procedure 1:
function getMax ( arr )
{
// returns the maximum value in the array arr
// returns undefined if array is empty
var max = Number.MIN_VALUE;
for (var i = 0, j = arr.length; i < j; ++i)
{
thisval = arr[i];
if (thisval > max) max = thisval;
}
return max;
}
Procedure 2:
function getMax ( arr )
{
// returns the maximum value in the array arr
// returns undefined if array is empty
var j = arr.length;
if (j == 0) return undefined;
var max = arr[0];
for (var i = 1; i < j; ++i)
{
thisval = arr[i];
if (thisval > max) max = thisval;
}
return max;
}
I'm wondering which would be considered better in terms of efficiency, compactness, readability, cleverness, maintainability, handling corner cases, etc. In other words, which one of those would I used if I was in a coding interview?
Aucun commentaire:
Enregistrer un commentaire