Course Content
JavaScript Data Structures
JavaScript Data Structures
2. Fundamentals of JavaScript Objects
Understanding Objects in JavaScriptCreating Objects in JavaScriptWorking with Nested Object PropertiesChallenge: Create and Structure an ObjectAccessing Object Properties in JavaScriptChallenge: Retrieve Object Property ValuesManaging Object PropertiesChallenge: Modify and Extend an ObjectDefining Object MethodsUsing Properties within MethodsChallenge: Work with Object MethodsFundamentals of JavaScript Objects Sum-Up
3. Advanced Object Manipulation Techniques
Iterating Over Object Properties with the for...in LoopChallenge: Explore Object Properties with IterationUsing hasOwnProperty() to Check Object PropertiesChallenge: Object Property Iteration with hasOwnProperty()Cloning and Merging Objects with the Spread OperatorChallenge: Combine Objects with the Spread OperatorDestructuring Objects for Cleaner CodeChallenge: Extract Data with Object DestructuringAdvanced Object Manipulation Sum-Up
4. Mastering JavaScript Arrays
Understanding JavaScript ArraysChallenge: Access Array ElementsModifying Arrays and Accessing ElementsChallenge: Modify Array ElementsIterating Over Arrays with the for LoopChallenge: Loop Through Arrays with forUsing the for...of Loop for Array IterationChallenge: Efficient Array Iteration with for...ofMastering JavaScript Arrays Sum-Up
5. Advanced Array Methods and Transformations
Transforming Arrays with the map() MethodChallenge: Modify Array Elements Using map()Filtering Arrays with the filter() MethodChallenge: Select Specific Data Using filter()Finding Elements in an Array with the find() MethodChallenge: Search for Items Using find()Sorting Arrays with the sort() MethodChallenge: Sort and Extract Data with sort()Advanced Array Methods and Transformations Sum-UpCourse Summary
Challenge: Work with Object Methods
Task
You're provided with an object representing a car's details. Your task is to create a method within the object that calculates the car's total price. The car's total cost is calculated by adding the base price and the sum of additional options.
- Inside the
calculateTotalPrice
method, use thethis
keyword to access the car'sbasePrice
. - Use the
this
keyword to access the options (leatherSeats
,sunroof
, andnavigationSystem
) from theoptions
object. - Calculate the total price by adding the base price and the sum of all options.
- Log the total price as the method's result.
const car = { make: "Ford", model: "F-150", basePrice: 72000, options: { leatherSeats: 2400, sunroof: 100, navigationSystem: 1650, }, calculateTotalPrice() { const totalPrice = ___ ; console.log("Total price is", totalPrice); }, }; car.calculateTotalPrice();
Expected output:
- Inside the method, use
this.basePrice
to access the base price. - Use
this.options
to access the options object. - You can use the dot notation to access option properties (e.g.,
this.options.leatherSeats
).
const car = { make: "Ford", model: "F-150", basePrice: 72000, options: { leatherSeats: 2400, sunroof: 100, navigationSystem: 1650, }, calculateTotalPrice() { const totalPrice = this.basePrice + this.options.leatherSeats + this.options.sunroof + this.options.navigationSystem; console.log("Total price is", totalPrice); }, }; car.calculateTotalPrice();
Everything was clear?
Thanks for your feedback!
Section 2. Chapter 11