Basic Data Structures - Modify an Array Stored in an Object

Tell us what’s happening:

Can anyone tell me why my code is returning an empty string?

Your code so far

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

function addFriend(userObj, friend) {
  // Only change code below this line
let result = [];
for (const props1 in userObj){
  if (userObj[props1] == 'data' ){
    for (const props2 in userObj[props1]){
      if(userObj[props1][props2]== 'friends') {
        let resultT = userObj[props1][props2].push(friend);
        result = [...resultT];
      }
    }
  }
}
return result
  // 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/120.0.0.0 Safari/537.36

Challenge Information:

Basic Data Structures - Modify an Array Stored in an Object

Take a closer look what value has props1 and props2 within the loop.

let result = ;
for (const props1 in userObj){
if (userObj[props1] == ‘data’ ){
for (const props2 in userObj[data]){
if(userObj[data][props2]== ‘friends’) {
let resultT = userObj[data][friends].push(friend);
console.log(resultT)
result = […resultT];
}
}
}
}
return result

still not working.

Try adding a couple of console.log() calls, to see what props1 and props2 contain within loops.
For example:

  for (const props1 in userObj){
    console.log(props1)

Thanks. I figured it out.

Btw, can you tell me why this code - (resultT = userObj[props1][props2].push(friend); console.log(resultT); ) returns the integer value 4 but this code - ( resultT = userObj[props1][props2] ; resultT.push(friend); ) returns the desired array ?

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