- To work in pairs solving real-world programming problems.
- To practice creating, accessing and processing data structures, Objects and Arrays, in JavaScript.
- Practice modeling types of things, animals, using Objects.
- Practice storing Objects in Arrays, and searching, adding, replacing, and removing these Objects based on criteria.
- Getting use to using
console.log
to inspect your work and debug your code.
- variables
- looping
- Objects
- Arrays
- Functions
If you don't know any of these concepts, see a Teacher or TA before starting this project!
- Open your Cloud9 workspace for your website project.
- In the terminal run the command,
os install
. - Note, the first time you run
os install
, you'll be asked to login to GitHub. Be certain to type your credentials carefully. - From the list of projects, use the up/down arrows to select
matchy
- Note the newly installed
projects/matchy
folder in your website workspace.
- Once installed, navigate to and open the files
projects/matchy/data.js
andprojects/matchy/functions.js
. We'll code the project exercises in these two files, starting with Part 1 indata.js
, then Part 2 infunctions.js
. - To run your test, right click on
index.spec.html
and select preview. To view your console logs, click on the pop out box (with the arrow) in the top right corner and open the Console in the Chrome Developement Tools.- Open the developer console by right clicking anywhere and choosing
inspect element
. Click on theconsole
tab in the panel that opens up. - or -
- Hit the F12 key.
- Hit the CMD + Option + I keys for Mac
- Open the developer console by right clicking anywhere and choosing
- Preview the
index.html
file to view your animals and their friends!
All work in this section will be done in data.js
As we code, save and refresh the browser tab running Matchy. Your work will begin to create a web UI, signaling you've correctly coded the exercises. You will also be logging your data to the console, so you can check this output to make sure your code works as expected.
- Open up the file data.js.
- Create a variable named
animal
and assign it to an empty object. - Using dot notation give
animal
a property namedspecies
with a value of any animal species. - Using bracket notation give
animal
a property calledname
with a value of your animal`s name. - Using either notation, give
animal
a property callednoises
with a value of empty array. - Print your
animal
Object to the console by adding,console.log(animal);
, - It should be something like:
{ species: 'duck', name: 'Jennifer', noises: [] }
- Create a variable named
noises
and assign it to an empty array. - Using bracket notation give
noises
it's first element. A string representing a sound your animal might make. - Using an array function add another noise to the end of
noises
. - Go to the array documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array?redirectlocale=en-US
- Look through the functions until you find the one that will place an element at the begining of the array.
- Add an element to
noises
using this function. - Using bracket syntax again, add another element to the end of
noises
. Make sure that the way you do this step would work no matter how many elementsnoises
had. In other words, don't hard code the position of the new element. -
console.log
the length ofnoises
-
console.log
the last element innoises
again without hard coding the index. -
console.log
the whole array. - Does it look right?
- Using bracket syntax, assign the
noises
property onanimal
to our newnoises
array. - Using any syntax add another noise to the
noises
property onanimal
. -
console.log
animal
. - Does it look right?
- What are the different ways you can access properties on objects?
- What are the different ways of accessing elements on arrays?
It's super important to give your brain and yourself a rest when you can! Grab a drink and have a think! For like 10 minutes, then, BACK TO WORK! :)
- Create a variable named
animals
and assign it to an empty array. -
push
ouranimal
that we created toanimals
. You should now see your first animal appear on yourindex.html
page! -
console.log
animals
. What does it look like? - Create a variable called
duck
and assign it to the data:
{ species: 'duck', name: 'Jerome', noises: ['quack', 'honk', 'sneeze', 'woosh'] }
-
push
duck
toanimals
-
console.log
animals
. What does it look like? - Create two more animal objects each with a species, a name, and at least two sounds sounds and add each one of them to `animals.
-
console.log
animals
, and,console.log
the length ofanimals
. Is everything looking right?
Imagine that our website has a profile page for each animal. On this profile page we can see a list of each animal's friend on the website. Just like how people have a list of friends or followers on facebook or instagram. What would be a good data structure to hold this list of friends?
- Choose a data structure for this list of friends.
- Write a comment in your code that explains why you chose this data structure.
- Create a variable called
friends
and assign it to the data structure that you chose. - Take a look at the documentation for
Math.random
here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random - Write a function called
getRandom
that takes ouranimals
array and returns a randomindex
of the input array, usingMath.random
- Using a random index from this function that you just created, get a random animal and add its
name
tofriends
. -
console.log
friends
. - Using bracket notation, add the
friends
list as a property also namedfriends
on one of the animals in theanimals
array -
console.log
your work.
All work in this section will be done in functions.js
In this file, we'll create some Functions to work with our data created Part 1, which we coded in the file data.js
.
These Functions will pass in all needed parameters, meaning, we will not reach into the global scope to access animals
.
- Open up the file
functions.js
in your editor. - Implement a function declaration called
search
that:
- Takes a paramater representing an Array of
animals
. - Takes a paramater representing a String, the name of an animal on which to perform a search.
- Looks through the
animals
Array, and returns the animal's Object if an animal with that name exists. - Returns
null
if no animal with that name exists
- Use the search bar at the top of the page to make sure your function works.
- Write a function declaration called
replace
with a signature ofreplace(animals, name, replacement) { //... }
that:
- Takes 3 parameters, an Array of animals, a String representing the name of an animal on which to perform a search, and an Object that represents the replacement animal.
- If an animal with that name exists within the
animals
Array, replace it's entire Object with the replacement Object. - Otherwise do nothing.
- Preview the
index.html
page to test it on the website.
- Write a function declaration called
remove
with a signature ofremove(animals, name)
that:
- Takes 2 parameters, an Array of animals, and a name of an animal on which to perform a search.
- If an animal with that name exists within the
animals
Array, remove it.
- Test that it works on the website.
- Write a function declaration called
add
with a signature ofadd(animals, animal) { //... }
that:
- Takes 2 parameter, an Array of animals, and an Object representing a new animal to be added.
- Checks that the animal Object has a
name
property with a length > 0. - Checks that the animal Object has a
species
property with a length > 0. - Has a unique name, meaning no other animals have that name.
- Adds this new Object to the
animals
Array, only if all the other conditions pass. - Make sure it works.
This is called data validation and it's extremely important in web development!
Step back and think about how far you've come!! We are doing really hard stuff and if you've gotten here, you get it! This is awesome! It means you are well on your way to becoming a pro web developer. SWEET!
Test and submit your work
Now you're done! Time to submit your great work to Greenlight to get that sweet, sweet green light.
- Open up a new
bash
terminal - Run the command
os test
in the bash terminal
- Select your class
- Select your Matchy
- Make sure that you are passing all tests, or fix any failing tests
- When all your tests pass, run the command
os submit
- Select your class
- Select your Matchy
- Check Greenlight to ensure your grade updated correctly
- You're done!