0

I was trying this simple freecodecamp.org exercise, and was running into a problem with my solution.

What I can't work out is why these two functions return different results:

let someArray = {
  Person: {
    age: 27,
    online: true
  }
};


function works(obj) {
  return ('Person' in obj); 
}

console.log("works returns:" + works(someArray));

function doesNotWork(obj) {
  return  
    ('Person' in obj); 
}

console.log("doesNotWork returns:" + doesNotWork(someArray));

The results of running this on node.exe v10.8.0 results of execution

Why is the second function returning undefined.

Preet Sangha
  • 61,126
  • 17
  • 134
  • 202

1 Answers1

2

You get undefined because there is a new line after return so it return undefined from doesNotWork function. Put that in a same line and it works:

let someArray = {
  Person: {
    age: 27,
    online: true
  }
};

function works(obj) {
  return ('Person' in obj);
}

console.log("works returns:" + works(someArray));

function doesNotWork(obj) {
  //should be in a same line
  return ('Person' in obj);
}

console.log("doesNotWork returns:" + doesNotWork(someArray));
Ankit Agarwal
  • 28,439
  • 5
  • 29
  • 55