Practice and reinforce the concepts from Lesson 8
In this activity, you will:
DO NOT DELETE the existing files in the template:
ONLY EDIT the necessary files.
Total Activity Time: 45-60 minutes
Time: 10-15 minutes
Steps to complete:
myPet
with 2 properties:
animal
(e.g., "dog", "cat", "hamster")name
(e.g., "Buddy", "Whiskers", "Nibbles")console.log()
to display the animal
propertyconsole.log()
to display the name
propertytip
Use dot notation to access object properties: myPet.animal
or myPet.name
Example object structure:
var myPet = {
animal: "your_choice",
name: "your_choice"
};
If you have done it correctly, it should look like this in the browser console (F12):
Assuming the value of the animal property is "pug" and the name is "Maya".
Time: 15-20 minutes
Challenge: Change the values in an object without directly changing the given object code
script.js
var movie = {
name: "Luca",
release: "June 18, 2021",
genre: "Comedy/Adventure",
duration: "1h 35m",
director:"Enrico Casarosa"
};
Steps to complete:
name
release
genre
duration
director
console.log(movie)
to display the modified objecttip Modify properties using dot notation after the object is created:
movie.name = "Your New Movie";
movie.release = "New Release Date";
// ... and so on
If you have done it correctly, it should look like this in the browser console (F12):
Assuming the movie is "Raya and the Last Dragon".
tip If there are multiple directors for the movie, you can assign an array:
movie.director = ["Director 1", "Director 2"];
Time: 20-25 minutes
Goal: Create a light bulb object with a toggle method that switches between On/Off states
Steps to complete:
Select the button element:
var lightBtn = document.querySelector('button');
Create the light object with these properties:
on
: set to false
toggle
: an arrow function that:
light.on = !light.on;
Inside the toggle arrow function:
if (light.on) {
lightBtn.textContent = "On";
lightBtn.classList.replace("btn-dark", "btn-warning");
} else {
lightBtn.textContent = "Off";
lightBtn.classList.replace("btn-warning", "btn-dark");
}
In index.html, add onclick to the button:
<button onclick="light.toggle()">Off</button>
tip Arrow function syntax for object methods:
var light = {
on: false,
toggle: () => {
// your code here
}
};
:information_source: Info Helpful Resources
It should look like this before you click on the button:
After you click on the button:
If you click on the button again:
Make sure your Object projects include:
myPet.name
not myPet[name]
)onclick="light.toggle()"
to your HTML buttonbtn-dark
and btn-warning
classes exist in your CSS:warning: Important! Test all your exercises before submitting:
- Exercise One: Both properties should log to console
- Exercise 2: All movie properties should be updated
- Exercise 3: Button should toggle between On/Off states
When you have completed all three Object exercises, submit them using the link below: