In this section we will explore how to represent our eFarmony data as a JavaScript object.
You will be placing all your code into the scripts.js file.
###Animal User Model
This object will be the model of a single animal user. Extra points if you get the pun in the last sentence.
An object to hold our data model...
- Create a variable, name it
animal, and assign it an object literal.
With Dot Notation…
- Add a property called
usernameand assign it a value. - Ensure that your
usernameproperty exists in animal by inspecting it in the console.
With Bracket Notation…
- Add a property called
taglineand give it a value. - Check that your property exists in the animal object by inspecting it in the console.
- Create a variable called
noisesand assign it an empty array[] - Add the
noisesarray to your object. - Inspect your handiwork! Your object should look something like this:
{ username: 'DaffyDuck', tagline: 'Yippeee!', noises: [] }
- Loop through the properties of your animal object.
- Count everytime it loops to keep track of the number of properties on your object.
- Write an if/else statement in your loop:
- If the key is
username, console.log('Hi my name is ' + ___) //fill in with object's username value. - If the key is
tagline, console.log('I like to say ' + ___) //fill in with object's tagline value.
- If the key is
- What happens if you return 'Hi my name is ' + ___ instead of using console.log() inside the loop?
Let's go over some concepts:
- What are the different ways you can add properties and values to objects?
- Which of these methods would you use if you wanted to add a property to an object that had a weird symbol (think '&')?
- What about if the property is a variable, how does that change the syntax?
//Functions exercises
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
AnimalTestUserfunction, create a check that sees how many arguments are passed. If there is more than one argument, create a property,otherArgsthat is an array of the remaining arguments. Note: theargumentskeyword 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,
AnimalCreatorthat 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 exceptnoisesandfriends, 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,
addFriendthat takes an animal object (like the one returned from theAnimalCreatorfunction) 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
addFriendfunction 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
myFarmcollection 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 calledmatchesthat 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 thefriendsarray and adds it to thematchesarray. 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']
// }