I am new at coding and I don't understand why my code doesn't work for the last test of this Coding Game problem : https://www.codingame.com/ide/puzzle/temperatures
Language : Javascript
Last test is : Display 0 (zero) if no temperatures are provided
Problem : In this exercise, you have to analyze records of temperature to find the closest to zero.
Rules: Write a program that prints the temperature closest to 0 among input data. If two numbers are equally close to zero, positive integer has to be considered closest to zero (for instance, if the temperatures are -5 and 5, then display 5).
Game Input: Your program must read the data from the standard input and write the result on the standard output. Input Line 1: N, the number of temperatures to analyze Line 2: A string with the N temperatures expressed as integers ranging from -273 to 5526
Output: Display 0 (zero) if no temperatures are provided. Otherwise, display the temperature closest to 0. Constraints 0 ≤ N < 10000
Here is my code:
`/**
- Auto-generated code below aims at helping you parse
- the standard input according to the problem statement. **/
`
const n = parseInt(readline()); // the number of temperatures to analyse
var inputs = readline().split(' '); // this is the array of split temperature
var t;
for (let i = 0; i < n; i++) {
t = parseInt(inputs[i]);// a temperature expressed as an integer ranging from -273 to 5526
}
var temperatureOfZero = 0;
function closest(array){
return array.reduce((a, b) => {
let aDiff = Math.abs(a - temperatureOfZero);
let bDiff = Math.abs(b - temperatureOfZero);
if (Array.isArray(array) && array[0] === "" ) {
return 0;
}
else if ( aDiff == bDiff){
if (a > b){
return a;
}else{
return b;
}
}//end of first else if (aDiff == bDiff)
else if (aDiff > bDiff){
return b;
}else{
return a;
}
}); }
let result = closest(inputs);
console.log(result);`
Input test 1:
5
1 -2 -8 4 5
Output test 1:
1
Input test 5 (Display 0 (zero) if no temperatures are provided
0
Output test 5 that I should get
0
Output that I get when I run my code:
Failure
Found: Nothing
Expected: 0
Thx you in advance for the explanation