Tuesday, 18 October 2016

Fun little challenge (Temperatures closest to 0)

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/

var n = parseInt(readline()); // the number of temperatures to analyse
var temps = readline(); // the n temperatures expressed as integers ranging from -273 to 5526
var b = function(a){
    return Math.abs(a);
}
var closest = temps.split(" ").reduce((p,c)=>(b(p)===b(c))?(p<c)?c:p:(b(c-0)<b(p-0)?c:p));


// Write an action using print()
// To debug: printErr('Debug messages...');

print((closest) ? closest : 0);