I am trying to perform the following code:
function oldestAges(ages){
if (ages == []){
return [0,0];
}else{
var max = Math.max.apply(null, ages);
ages.splice(ages.indexOf(max), 1);
var max2 = Math.max.apply(null, ages);
return [max2,max];
}
}
However, when testing [] as ages, the expected was '[0, 0]', instead got: '[-Infinity, -Infinity]'
Also, is there a much easier way to accomplish the same task? I ask because using R I could get the same result in much less number of lines. I am noob in javascript still.
==to compare arrays by their contents. The operator compares only by object identity, so one array will never ever be==to another array.if(ages.length == 0)to test for an empty array.if (ages == []){- it will always returnfalse