Https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/modify-an-array-stored-in-an-object

Tell us what’s happening:
I wrote the code the code as per conditions mentioned, the output is correct.
But Compiler says its not correct, though the output is correct.
I checked out solution in get hint section its using push method and I am accessing the desired location using dot notation and array indexing.
I can not where am I wrong.

Your code so far


// Take a look at the object we've provided in the code editor. The user object contains three keys. The data key contains five keys, one of which contains an array of friends. From this, you can see how flexible objects are as data structures. We've started writing a function addFriend. Finish writing it so that it takes a user object and adds the name of the friend argument to the array stored in user.data.friends and returns that array.

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
userObj.data.friends[3] = friend;
return userObj.data.friends;
// Only change code above this line
}

console.log(addFriend(user, 'Pete'));
console.log(user);

Your browser information:

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

Challenge: Modify an Array Stored in an Object

Link to the challenge:

Though I’m not that experienced in javascript, I’ll try answering your question.
The challenge is indirectly asking you to add element friend at the end of the array. By specifying 3, that’s no longer the end of the array, but instead it is location 3 specifically. I guess it would be better is you used push since that would add the element friend to the end of the array instead of at the position 3. Of there was another element in that position, the object friend would have took its position and modified the object previously held in the third part of this array. Hope this helps. Good luck.

1 Like

got your point, ty very much.

1 Like