javascript - Math.max method on array with equal values -


i'm working on coderbyte code, , noticed when try max item in array of equal values undefined returned. when logging min value logs 80 , not undefined. why this?

updated code:

function norepeat(arr) {     tmp = []     if (arr.length === 2 && arr[0] === arr[1]) {         return arr;     }     (var = 0;i<arr.length;i++) {         if (tmp.indexof(arr[i]) === -1) {             tmp.push(arr[i])         }     }     return tmp } function secondgreatlow(arr) {     arr = norepeat(arr).sort(function (a,b) {return a-b;});     var low = arr[arr.indexof(math.min.apply(math,arr))+1];     console.log("low",low);     var high = arr[arr.indexof(math.max.apply(math,arr))-1];     console.log("high",high);     return low +" "+high; } console.log(secondgreatlow([80,80])); 

output:

"low" 80  "high" undefined "80 undefined" 

that's, actually, ok. how want find second largest \ smallest number in array of 2 similar numbers? should output "no solution" or else. there no solution empty array.

function secondgreatlow(arr)  {     arr = norepeat(arr).sort(function (a,b) {return a-b;});     if (arr.length < 2)         return "no solution";     console.log("low ", arr[1]);     console.log("high ", arr[arr.length - 2]);     return low + " " + high; } 

you don't need math min , max functions array sorted , values unique. need take second beginning , second end.

also, don't need part calculated right algorithm.

if (arr.length === 2)  {     return arr[1] + " " + arr[0]; } 

for example, have array [1, 1, 2].
remove repetitions , [1, 2].
algorithms returns low = arr[1] = 2 , high = arr[2 - 2] = arr[0] = 1.
answer correct - 2 second minimum number , 1 second largest.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -