Modify an Array Stored in an Object lesson

Hi, so I have completed this challenge after I saw the hint with two simple lines like suggested, but at first I tried this.

Two questions.

  1. Why is this code not working?

  2. Why Iterating isn’t necessary to complete this challenge? Ty.

    Your code so far

let user = {
name: 'Kenneth',
age: 28,
data: {
  username: 'kennethCodesAllDay',
  joinDate: 'March 26, 2016',
  organization: 'freeCodeCamp',
  friends: [
  location: {
    city: 'San Francisco',
    state: 'CA',
    country: 'USA'

function addFriend(userObj, friend) {
// Only change code below this line
for (let users in userObj) {
if (userObj[users].hasOwnProperty('data')) {
// Only change code above this line

console.log(addFriend(user, 'Pete'));
  **Your browser information:**

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

Challenge: Modify an Array Stored in an Object

Link to the challenge:

I suggest you add a console.log inside the loop to check what users value and usersObj[users] value are, like this
console.log(users, usersObj[users])

and then consider if usersObj[users can have the orioerty data

1 Like

i have done it console.log(users, userObj[users]) this gives me that data is an object.
console.log(userObj[users]) this gives me that 28 is an object…

I am not sure whether it’s wrong or how come it shows it just like that and not fully like console.log(user, userObj[users]).

28 is certainly not an object

to answer the other question, you don’t need a loop because you have only one property to check, and you know which one. But it will not be on usersObj[users], as you can see from your logging that usersObj[user] doesn’t have a data property

How can I know these stuff like when an object doesn’t have a property, like in our example only by checking console.log()?

So when do I use loop? when I don’t know which property to check?

if you need a loop or not depends on the problem you are solving.

You know what object you are checking, you are given the object, so you just need to access the object in the right way and do as requested

Okay thanks for your answer hope it will get more cleared to me a long my learning!

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