-1

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

Hydrocoder
  • 1
  • 1
  • 2
  • Please, provide the input data of the test and the error description. If you want some help don't force people to click in the link and go to another page in order to understand your problem. – Iogui Dec 29 '20 at 20:49
  • 1
    What happens when you run your code? Also, please post your code as text (code tag) so that it can be ran by others. – jprusakova Dec 29 '20 at 20:51
  • @logui @ jprusakova Sorry about that. I edited my post with the input data of the last test and the error description. Thx you for you help. If you need anything else, let me know :) – Hydrocoder Dec 30 '20 at 15:08

3 Answers3

0

I think you need to return 0 when the array is empty. Something like:

function closest(array){

  if(array.length === 0) return 0;

  return array.reduce((a, b) => {
    ...
  }); 
}
0

Don't know if you already found out the solution. I found out about codingame this week and I was doing that same exercise and I came to a solution. I will share with you.

function closestToZero(numbers) {

  if(!numbers.length) return 0;

  let closest = Math.max(...numbers);
  for (let i = 0; i < numbers.length ; i++) { 
    if (Math.abs(numbers[i]) < Math.abs(closest)) {
      closest = numbers[i];
    } else if (Math.abs(numbers[i]) === Math.abs(closest)) {
       if (numbers[i] > closest) closest = numbers[i];
    }
  }
  return closest;

};

let inpArr = [0, -1, 1, 42];
console.log(`inpArr: ${inpArr.join(', ')} result: ${closestToZero(inpArr)}`);

inpArr = [];
console.log(`inpArr: ${inpArr.join(', ')} result: ${closestToZero(inpArr)}`);

inpArr = [30, -30, 43, 84, 102, -31];
console.log(`inpArr: ${inpArr.join(', ')} result: ${closestToZero(inpArr)}`);

inpArr = [93, -11, 22, -15, 43, 3, -2];
console.log(`inpArr: ${inpArr.join(', ')} result: ${closestToZero(inpArr)}`);

inpArr = [-30, -3, -6, -9, 44];
console.log(`inpArr: ${inpArr.join(', ')} result: ${closestToZero(inpArr)}`);

I don't know if there is a more accurate way of doing this, but after a few moments of thinking, this was the way I found to pass the 6 tests

0xLogN
  • 3,289
  • 1
  • 14
  • 35
LauraS
  • 1
0

LauraS solution is pretty smooth, but is not considering what happen when you have a 0 inside the array, also CodinGame is not taking that possibility, that's why is passing all tests, but is cleaner if you add ts[i] === 0 return ts[i] in my opinion

Tavo
  • 1
  • 1
    Your answer should rather be a comment on LauraS answer, so their solution be improved. – jmeinlschmidt Apr 10 '22 at 00:39
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31499556) – Nick Vu Apr 12 '22 at 09:53