Testing Object doesn't return any property

Tell us what’s happening:
Describe your issue in detail here.

Hi, I have created this script to verify the Object testing, but if I run it on a browser it never returns any property. Can you please help me to find where the error is? Many thanks in advance.

Pierluigi Cecchi

  **Your code so far**



let out = "";

function checkObj(obj, checkProp) {
// Only change code below this line

const lookup =   {
   "alpha": "Adams",
   "bravo": "Boston",
    "charlie": "Chicago",
    "delta":"Denver",
    "echo": "Easy",
    "foxtrot": "Frank",
}

if (obj.hasOwnProperty(checkProp)) {
  return obj[checkProp];
}
else { return "Not Found";}
return "Change Me!";
}

out = checkObj("lookup" , "delta");

console.log(out);

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Testing Objects for Properties

Link to the challenge:

Hi @pierluigicecchi !

You have a few issues here.

No.1:
You are referencing a string here

But your goal is to reference an object.
You need to remove the quotes.

No.2:
Your object of lookup is defined inside the function.
If you tried this function call

checkObj(lookup, "delta")

Then you should receive an error message telling you that lookup is not defined.
To get rid of the error message you need to make that object a global object.

This is the correct code to return the result you are looking for.

let out = "";

const lookup = {
  alpha: "Adams",
  bravo: "Boston",
  charlie: "Chicago",
  delta: "Denver",
  echo: "Easy",
  foxtrot: "Frank"
};

function checkObj(obj, checkProp) {
  // Only change code below this line

  if (obj.hasOwnProperty(checkProp)) {
    return obj[checkProp];
  } else {
    return "Not Found";
  }
}

out = checkObj(lookup, "delta");

console.log(out);

Hope that clears it up!

Many thanks.

It works perfectly.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.