In this section, you'll practice creating, accessing, modifying, and working with objects in JavaScript. You'll write programs that solve problems by manipulating object properties and methods, as well as working with nested objects and arrays of objects.
In objects.js, for each prompt, write a JavaScript program that solves the problem using objects. Make sure to run the program in your terminal to check if it works as expected.
-
Create a Simple Object
Write a program to create an object namedcarwith propertiesmake,model, andyear. Print the object. -
Access Properties
Write a program that prints themakeandmodelproperties of thecarobject. -
Add a New Property
Add acolorproperty to thecarobject and print the updated object. -
Delete a Property
Delete theyearproperty from thecarobject and print the updated object. -
Modify a Property
Update thecolorproperty in thecarobject to"red"and print the updated object. -
Check for Property
Write a program to check if thecarobject has adoorsproperty. Printtrueorfalse. -
Iterate Through an Object
Use afor...inloop to print all the keys and values in thecarobject.
-
Create a Nested Object
Create an objectbookwith propertiestitle,author, anddetails. Thedetailsproperty should be another object containingpublisherandyear. Print the object. -
Access Nested Properties
Write a program to print thepublisherandyearof thebookobject. -
Modify Nested Properties
Update theyearproperty insidedetailsof thebookobject to the current year. Print the updated object. -
Add Nested Properties
Add a new propertypagesto thedetailsobject insidebook. Set it to350and print the updated object. -
Delete Nested Properties
Delete thepublisherproperty from thedetailsobject insidebook. Print the updated object.
-
Create an Array of Objects
Create an arraystudentswith three objects, each representing a student. Each object should have propertiesname,age, andgrade. Print the array. -
Access Objects in Array
Write a program to print thenameof the second student in thestudentsarray. -
Modify an Object in an Array
Update thegradeof the first student in thestudentsarray to"A". Print the updated array. -
Add a New Object to the Array
Add a new student object to thestudentsarray and print the updated array. -
Remove an Object from the Array
Remove the last student object from thestudentsarray and print the updated array.
-
Copy an Object
Use the spread operator to create a copy of thecarobject and modify itsmakeproperty. Print both the original and the copied object. -
Merge Two Objects
Create two objects,personandjob, with some properties. Merge them into a new object and print it. -
Object Methods
Create an objectcalculatorwith methodsadd,subtract,multiply, anddivide. Each method should take two numbers as arguments and return the result. Use thecalculatorobject to perform operations and print the results.
- Complete all drills in the file.
- Push your work to your forked copy, create a pull request on the original repo, and then submit your URL on Google Classroom.
- Be prepared to explain how your code works.
Happy coding and good luck! 🚀