Basic Data Structures - Generate an Array of All Object Keys with Object.keys()

Dumb question but I’ve completed the challenge, however I just can’t get my head around why the (obj) is there when it’s referenced nowhere else in the code. Anyone care to enlighten me? Thanks in advance!

Part of the code I’m referencing:
function getArrayOfUsers(obj) {
// Only change code below this line
return Object.keys(users);

  **Your code so far**
let users = {
Alan: {
  age: 27,
  online: false
},
Jeff: {
  age: 32,
  online: true
},
Sarah: {
  age: 48,
  online: false
},
Ryan: {
  age: 19,
  online: true
}
};

function getArrayOfUsers(obj) {
// Only change code below this line
return Object.keys(users);



// Only change code above this line
}

console.log(getArrayOfUsers(users));
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:104.0) Gecko/20100101 Firefox/104.0

Challenge: Basic Data Structures - Generate an Array of All Object Keys with Object.keys()

Link to the challenge:

You should use obj instead

Your solution is hardcoded that’s why you passed the test case for this question, when you call the function using different object, it will still return users

Let’s say I have a new object here called items

let items = {
  Itemone: {
    price: 27,
    tax: false
  },
  Itemtwo: {
    price: 32,
    tax: true
  },
  Itemthree: {
    price: 48,
    tax: false
  },
};

If I call your function
getArrayOfUsers(items);

I will still get a list of users but not items.

you mean this?

it’s a function parameter

I can do stuff like

let a = 3;

const someFunction = (iAmParameterIcanBeWhateverYouPass) => {
  return a * 2;
}

console.log(someFunction(a))//6

It will work and this stuff iAmParameterIcanBeWhateverYouPass
not referenced anywhere in the code

// This is a definition of an object.
// users is the name of the variable, and it has a scope.
let users = { ... }    

// This is a definition of a function
// getArrayOfUsers is the name of the function, and this also has a scope.
// obj is a function's parameter and its a variable, and this has a scope too.
// NOTE that you cannot use obj outside the function getArrayOfUsers, because of the scope.
function getArrayOfUsers(obj) {
    // ...
}

// getArrayOfUsers(users) is invoking the function using the users as an argument.
console.log(getArrayOfUsers(users));

When you pass users to the function, within the function it is assigned to obj. So what ever you do to the obj, it is connected (referencing) to the users. In this case, you read from obj and its same as reading from users. But, outside the function obj has no identity (you can try using it and see what happens).

Your question is more related to variable scope (you can also refer your course notes on variable scope). Ideally, you should use the variable defined within the function though you can use the variable from a higher scope.

1 Like

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