0

Am trying to read only very first element of friends array and add it to matches array for each object. If I add matches property directly on object. Then it returns expected result. But if I use a function addmatches to add matches array property. Then matches array value is coming same for each animal object.

var AnimalCreator = function(username, species, tagline, noises) {
  var friends = [];
   //var matches = [];
  return {
    username: username,
    species: species,
    tagline: tagline,
    noises: noises,
    friends: friends,
    //matches: matches
  }
};

var sheep = AnimalCreator('Cloud', 'sheep', 'You can count on me!', ['baahhh', 'arrgg', 'chewchewchew']);
var rabbit = AnimalCreator('Sky', 'Rabbit', 'I like jumping!', ['haha', 'heehee']);
var tiger = AnimalCreator('Tim', 'Tiger', 'I am strong!', ['Grrrr', 'grrrrrr!']);
var bear = AnimalCreator('Poo', 'Bear', 'I am your friend', ['wowo', 'whwhwh']);

var addFriend = function(animal, friend) {
  animal.friends.push(friend.username);
};

addFriend(sheep, rabbit);
addFriend(sheep, tiger);
addFriend(tiger, bear);

var myFarm = [sheep, rabbit, tiger, bear];

var addmatches  = function(farm){
  var matches = [];
  for(var i=0;i < farm.length; i++){
    farm[i].matches = matches;
  }
};

addmatches(myFarm);
var giveMatches = function(farm) {
  for (var i = 0; i < farm.length; i++) {
    if (farm[i].friends.length > 0) {
      farm[i].matches.push(farm[i].friends[0]);
    }
  }
};
giveMatches(myFarm);
console.log(myFarm);
3
  • Well, what is your addMatches function supposed to do? As of right now, assigning the same matches array to every animal is exactly what it does, and that's all it does. Commented Feb 12, 2019 at 17:23
  • addMatches function is adding 'matches' property to the object. Commented Feb 12, 2019 at 17:26
  • if I add matches propperty directly on object by uncommenting matches property in AnimalCreator function. And commenting out addmatches function. Notice matches array for each object . Why it's giving different values for matches property in these 2 scenarios Commented Feb 12, 2019 at 17:30

2 Answers 2

2

matches is a reference to an empty array and your code is assigning same reference for every farm object ( farm[i].matches = matches; ), you can try by assigning empty array directly like

farm[i].matches = [];

Your commented code: The function AnimalCreator is creating a new array instance on every call and every animal object is referencing to a unique array, that's why when you directly add matches, it works as expected.

Hope this will help!

Sign up to request clarification or add additional context in comments.

Comments

2

This is because array is passed by referance, so every farm object is pointing to the same array by this "farm[i].matches = matches;". To fix this you can do multiple tihings.

  1. Change "farm[i].matches = matches;" to "farm[i].matches = [];" (Best option as of now).
  2. Move "var matches = [];" inside for loop and use "let" insted of "var".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.