Codechef unfairly make my code test case failed

on question add two numbers i make the correct code but the submission says i failed. please correct your buggy submission test program codechef

here is my code

function addTwoNumbers(tests) {
    for (let i = 1; i < tests.length; i++) {
        let arr = tests[i].split(" ");
        console.log(Number(arr[0]) + Number(arr[1]));
    }
}

It’s not bugged. It’s a normal JavaScript behavior.

Look at the function that gets the inputs:

process.stdin.setEncoding('utf8');
process.stdin.on('data', function(input) {
  const tests = input.split('\n');
  addTwoNumbers(tests);
});

What is it doing in this part?

const tests = input.split('\n');

It’s splitting the input based on new lines.
What if the input is like this?

2 ‘\n’
2 10 ‘\n’
5 12 ‘\n’

You will have a “tests” array like this:
tests = [“2”, “2 10”, “5 12”, “”]
It has length 4, not 3 because JavaScript is forcing another index just because that final new line (‘\n’). This means that your code is trying to sum from an empty index.

You had two options:

  1. Assuring you only take not empty lines like this:
function addTwoNumbers(tests){
    for (let i = 1; i < tests.length; i++) {
        if (tests[i] != ""){
            let arr = tests[i].split(" ");
            console.log(parseInt(arr[0])+parseInt(arr[1]));
        } 
    }
}
  1. Deliberately stating the number, which is included in the first index (best option):
function addTwoNumbers(tests){
    var T = parseInt(tests[0])
    for (let i = 1; i <= T; i++) {
        let arr = tests[i].split(" ");
        console.log(parseInt(arr[0])+parseInt(arr[1]));
    }
}

thx a lot it make sense now, that seriously is a bad and confusing way to put input parameters by codechef, i got stuck for a couple hours just because of that stupid newline at the end.

Do not worry.

I imagine you were mad, and it’s absolutely understandable. There will be more inputs like this (I had my time suffering with this too). But one learns a lot from these kind of problems from Competitive Programming and your language itself.

If you ask me how I figured it out, I had my suspects when my codes were throwing WA. But it relized the issue when I debugged a failing attempt and downloaded the input and output. Everything was ok, but the bottom that had “NaN”.

Then I figured it out.

So few problems are bugged (there still are, and feedback to the admins is valuable, as humans, they’ll fail). But most of these kind of problems will push you into wondering why.

“Did I overflow?”, “Did I use too much Memory”, “Did I not consider a corner case?”

I encorage you to keep learning, and being as critical and determinated as you seem to be!! :grin: