You will be placing all your code into the scripts.js file
- Write a function,
AnimalTestUser
, that has one string parameter,username
. It returns an object with a username property.
var testSheep = AnimalTestUser('CottonBall');
console.log(testSheep); //{ username: 'CottonBall' }
- In your
AnimalTestUser
function, create a check that sees how many arguments are passed. If there is more than one argument, create a property,otherArgs
that is an array of the remaining arguments. Note: thearguments
keyword is not a true array. Remember, it is an array-like object.
var testSheep = AnimalTestUser('CottonBall', {'loves dancing': true}, [1,2,3] );
console.log(testSheep); //{ username: 'CottonBall', otherArgs: [ {'loves dancing': true}, [1,2,3] ] }
- Write a constructor function,
AnimalCreator
that returns a single animal object. The constructor function has 4 parameters:username
,species
,tagline,
andnoises
. The animal object should have at least 5 properties:username
,species
,noises
,tagline
, andfriends
. The values should all be strings exceptnoises
andfriends
, which are arrays.
var sheep = AnimalCreator('Cloud', 'sheep', 'You can count on me!', ['baahhh', 'arrgg', 'chewchewchew']);
console.log(sheep);
// { username: 'Cloud',
// species: 'sheep',
// tagline: 'You can count on me!',
// noises: ['baahhh', 'arrgg', 'chewchewchew'],
// friends: []
// }
- Write a function,
addFriend
that takes an animal object (like the one returned from theAnimalCreator
function) and adds another animal object as a friend.
addFriend(sheep, cow);
console.log(sheep);
// { username: 'Cloud',
// species: 'sheep',
// tagline: 'You can count on me!',
// noises: ['baahhh', 'arrgg', 'chewchewchew'],
// friends: [{username: 'Moo', species: 'cow'...}]
// }
addFriend(sheep, llama);
console.log(sheep);
// { username: 'Cloud',
// species: 'sheep',
// tagline: 'You can count on me!',
// noises: ['baahhh', 'arrgg', 'chewchewchew'],
// friends: [{username: 'Moo', species: 'cow'...}, {username: 'Zeny', species: 'llama'...}]
// }
- Change your
addFriend
function to only add the username of the friend, not the whole object.
addFriend(sheep, cow);
console.log(sheep);
// { username: 'Cloud',
// species: 'sheep',
// tagline: 'You can count on me!',
// noises: ['baahhh', 'arrgg', 'chewchewchew'],
// friends: ['Moo']
// }
addFriend(sheep, llama);
console.log(sheep);
// { username: 'Cloud',
// species: 'sheep',
// tagline: 'You can count on me!',
// noises: ['baahhh', 'arrgg', 'chewchewchew'],
// friends: ['Moo', 'Zeny']
// }
- Create a
myFarm
collection of at least 3 animal objects. Give them some friends usingaddFriend
, too!
console.log(myFarm) //[{username: 'Cloud'...},{username: 'Zeny'...},{username: 'CottonBall'...}]
- Create a function,
addMatchesArray
, that takes a farm (array of animal objects) and adds a new property to each animal object calledmatches
that is an empty array. Hint: you will need a loop.
addMatchesArray(myFarm);
console.log(myFarm[0]); // { username: 'Cloud',
// species: 'sheep',
// tagline: 'You can count on me!',
// noises: ['baahhh', 'arrgg', 'chewchewchew'],
// friends: ['Moo', 'Zeny'],
// matches: []
// }
- Create a function,
giveMatches
, that takes a farm collection (aka an array of animal objects) that already has a matches property. It selects a name from thefriends
array and adds it to thematches
array. You can choose how the selection is made (random, the first element, etc). Make sure all your animal objects have friends.
giveMatches(myFarm);
console.log(myFarm[0]); // { username: 'Cloud',
// species: 'sheep',
// tagline: 'You can count on me!',
// noises: ['baahhh', 'arrgg', 'chewchewchew'],
// friends: ['Moo', 'Zeny'],
// matches: ['Zeny']
// }