Plz help in this code

I was solving a problem on code wars in JS and I am failing all the tests and I can’t find where I’m going wrong.
There is also no instruction for special cases.

I think there could be an issue with the tests.

Below is the link to the kata and the code I wrote:

function openOrSenior(data) {
  var result = []
  
  for(var i = 0; i < data.length; i++) {
  
   var r
    
   if (data[i][0] >= 55 && data[i][1] > 7) { r = "Senior"; } else { r = "Open";}

   if (data[i][1] > 26 || data[i][1] < -2) r= "Invalid Handicap";
    
   if (data[i][0] === undefined && data[i][1] === undefined) r = []; 
    
   if (data[i][0] === undefined || data[i][1] === undefined) r = "Incomplete Data";
    
   result.push(r)
 }
 console.log(result);
}

Also, is there a better/shorter way of writing the same code? I wrote an if/else if/else statement but it had some logic problem.

why do you use if multiple times? This means that the value r can be changed even after its set to Senior or Open

you also need to return the result.

There isn’t even a need for check invalid handicap or incomplete data.

Yeah I tried If/else if/else statement but there was an error with the logic.

function openOrSenior(data) {
  var result = []
  
  for(var i = 0; i < data.length; i++) {
  
   var r
    
   if (data[i][0] >= 55 && data[i][1] > 7) { 
     r = "Senior"; 
   } else if (data[i][1] > 26 || data[i][1] < -2) {
       r= "Invalid Handicap";
   } else if (data[i][0] === undefined && data[i][1] === undefined) {
       r = []; 
   } else if (data[i][0] === undefined || data[i][1] === undefined) {
       r = "Incomplete Data";
   } else { 
       r = "Open";
     }
    result.push(r)
 }
 console.log(result);
  return result;
}

openOrSenior([[54, 9], [21, 21], [75, 11], [1, 1], [90, 8], [90, 9], [55, 100], [90, 7], [, 8], [0, 0]])

//Returns ["Open", "Open", "Senior", "Open", "Senior", "Senior", "Senior", "Open", "Incomplete Data", "Open"]
//It prints out "Senior" for [55, 100] instead of "Invalid Handicap".

So thought about assigning r with values “Senior” or “Open” first and then change it’s value according to the case.

I read somewhere that it put the input and expected and instead it got “undefined”. Maybe I did’t understand it well.

It worked. I thought printing was enough and totally forgot about returning. Thanks :slight_smile:

this was caused by not using return at all. Your code handles an empty list perfectly fine.

Okay… :+1:

undefined is the default returned value by a function:

function example(){
    console.log("example");
}

// log returned result
console.log(example()); // undefined

this kata only includes valid input, so i would get rid of all the validation. One general thing, do validation first before assuming input is valid

Yes. If there is no return statement, the function will implicitly return “undefined”.

Yes, I always test it on codepen before attempting it.

Many times, I write perfect code but it still cannot pass all the tests so I also sometimes consider special cases.

It happened once and I put it here to discuss also. It was a long time ago…