In this lab, we'll practice using JavaScript objects by exploring the world of Jurassic Park!
Fork
andClone
this repositorycd
into the directorycode .
to open in VSCode- Create a new file with
touch
calledscript.js
. You'll do all your work in this file. Remember, you can test your work by typingnode script.js
in the terminal.
Create your own JavaScript object representing your favorite movie. I'm going to use Jurassic Park, obviously.
Example:
const myMovie = {
title: 'Jurassic Park',
director: 'Stephen Spielberg',
actors: ['Sam Neill', 'Laura Dern', 'Jeff Goldblum', 'Samuel L. Jackson'],
releaseYear: 1993,
duration: 128
}
- After you have created your movie object, console log the title of your movie using
dot notation
- Console log the director's name
- Console log the release year
- Use
bracket notation
in combination withdot notation
to access one of the actors and console log their name - Maybe your favorite movie came with an extended director's cut - write a statement that increases your movie object's duration by 25 minutes
What else can we do with objects in JavaScript, you may ask?
Today we visit Jurassic Park to explore the wild world of objects!
Copy and paste each of these JavaScript snippets into your JavaScript file. You'll be working off of these as we go.
In this first part, we're in Montana to check in with Dr. Grant on his Velociraptor dig.
// copy and paste into your script.js
const montana = {
scientist: 'Dr. Alan Grant',
depth: '10 meters',
dino: 'Velociraptor'
}
-
Create a variable called
guestOfHonor
and assign its value to the name of the scientist inmontana
. -
Access the value of the dino key found in the
montana
object and store it in a variable calledcleverGirl
.
Next, we head to Costa Rica, where Dennis Nedry and Dodgson (nobody cares) have already got a plan brewing to steal all of the dinosaur embryos...
// copy and paste into your script.js
const costaRica = {
programmer: 'Dennis Nedry',
contact: 'Lewis Dodgson',
payment: 750000,
specimens: [
'Tyrannosaurus Rex',
'Triceratops',
'Brachiosaurus',
'Velociraptor',
'Dilophasaurus',
'Gallimimus'
],
menthol: false
}
-
Store the array of specimens Nedry is supposed to steal in a variable called
barbasol
. -
Make a variable called
favoriteDino
and assign its value to your favorite dinosaur withinspecimens
. -
Add 750000 to Nedry's payment to reflect the total he'll receive if he succeeds.
Welcome to Jurassic Park! Things are starting off great! Spared no expense!
// copy and paste into your script.js
const jurassicPark = {
expenseSpared: 0,
staff: {
founder: 'John Hammond',
programmer: 'Dennis Nedry',
engineer: 'John Arnold',
security: 'Robert Muldoon',
scientist: 'Dr. Henry Wu'
},
guests: ['Dr. Alan Grant', 'Dr. Ellie Sattler', 'Dr. Ian Malcolm', 'Donald Gennaro'],
systems: [
{
area: 'Perimeter Fence',
volts: 10000,
online: true
},
{
area: 'T-Rex Paddock',
volts: 10000,
online: true
},
{
area: 'Velociraptor Pen',
volts: 10000,
online: true
}
]
}
-
Access the founder of Jurassic Park and store his name in an appropriately named variable.
-
The grandkids have arrived! Add "Tim" and "Lex" to the array of guests. You could assign the values to a specific index number of the array or you could look into .push().
-
Time for the tour! Everyone into the vehicles! Let's loop through and console log the entire updated list of guests.
-
Let's add the vehicles to the list of systems. Add the following object to the array of systems:
{
area: 'Tour Vehicles',
lockingMechanisms: false,
online: true
}
- Uh oh, fences are failing all over the park. Nedry said a few systems would go offline, didn't he? Switch the
online
keys tofalse
for the "Perimeter Fence", "T-Rex Paddock", and "Tour Vehicles" systems. Luckily the "Velociraptor Pen" is still online...
-
Check the vending machines! Nedry is nowhere to be found! Remove "Dennis Nedry" from the staff object. You could assign his role to an empty string or look into delete.
-
Well, when you gotta go, you gotta go. Remove "Donald Gennaro" from the list of guests. You could assign his specific index in the array an empty value, or you could look into .splice().
-
Looks like the "Velociraptor Pen" has gone offline too! Clever girl... Update it's
online
value tofalse
and remove "Robert Muldoon" from the staff object. -
Hold on to your butts. Sadly, remove "John Arnold" from the staff object. He wasn't able to get the systems back online.
- Dr. Sattler has found the main power console. Loop through our entire systems array and switch each
online
key totrue
. Mr.Hammond, I think we're back in business!