topskill.dev
CoursesTracks
0
All courses/Introduction to JavaScriptObject Methods and Destructuring
Objects and Built-ins
3 / 17
← Introduction to JavaScript
○What is JavaScript?○Your First JavaScript Code○Data Types○Type Checking○Missing values and deliberate emptiness○Inspect Values in the Console○Inspect the Queue Report○Trace a Faulty Calculation○Repair the Score Calculation✦Module Quiz
○Declaring Variables○let and const in Practice○Operators○Type Conversion○Group and Convert Expense Inputs○Calculate the Event Expense Total○Track a Changing Points Tally○Adjust a Ticket Price✦Module Quiz
○Comparisons and strict equality○Truthiness and combined conditions○Function inputs and results○Conditionals○Combined Conditions Before FizzBuzz○Challenge: FizzBuzz Announcer○Switch Statement○Switch a Venue Sign○Challenge: Power Loadout○For Loops○While Loop○Break and Continue○Stop a Ticket Scan○Challenge: Number Guess Referee○Rainfall Routing Walkthrough○Score Batch Report✦Module Quiz
○Defining Functions○Arrow Functions○Scope○Closures○Project - Calculator○Function Expressions○Ticket Pricing Walkthrough○Shipping Labels with Defaults○Seat Ledger Closure Walkthrough○Private Budget Ledger✦Module Quiz
○Arrays○Student Scoreboard○Challenge: Student Course Scoreboard○Array Mutation Methods○Array Slice and Concat○Array Iteration Methods○Multidimensional Arrays○Destructuring○Array Reverse and Sort○Array Fill○Array Find and Includes○Array Flat and FlatMap○Array Reduce, Some, and Every○Charging Rack Restock Report○Pantry Restock Plan✦Module Quiz
○Object Key-Value Pairs○Student Course Record●Object Methods and Destructuring○Object copies and optional properties○Map and Set Collections○Playlist Lookup○Number and BigInt Boundaries○Ticket Ledger○JSON○Throwing and catching errors○Math and Date○Date Basics○Supply Bin Audit○Volunteer Shift Audit○Workshop Attendance Feed○Parcel Scan Feed✦Module Quiz
○String Methods○Template Literals○Regular Expressions○Build a Reminder with Template Literals○Format Pantry Pickup Messages○Validate a Time Slot with Regex○Validate Storage Shelf Labels✦Module Quiz
○Higher-Order Functions○Callbacks○map, filter, and reduce○Student Register with map○Challenge: Student Register○Recursion○Project - Data Transform Pipeline○Extend the inventory report○Handle an Error-First Callback○Report Seat Reservation Results○Summarize Completed Bike Rides○Summarize Delivered Parcels✦Module Quiz
○A notebook object○OOP Introduction○Independent workshop passes○Classes○An inherited notice○Inheritance○A private seat counter○Encapsulation○Equipment desk checkout○Project: Library Checkout✦Module Quiz
○Callback Timing○Asynchronous JavaScript○Promise Rehearsal○Promises and Async Await○Offline API Coordination○Async API Simulation○Partial Results Bulletin○Project: Resilient Awards Feed○Retry a Local Delivery○Project: Reliable Score Delivery✦Module Quiz

Object Methods and Destructuring

Jordan's record stores a name and several scores. Read the name into local variables, then write helpers that can also handle another student's record.

Read properties into variables

javascript
const book = { title: "Dune", author: "Herbert" };
const { title, author } = book;
console.log(title, author);

The braces on the left are destructuring: read the named properties from book and create variables with those names. const { title: bookTitle } = book reads the same property but names the new variable bookTitle. It does not rename the property in the object.

A function can read properties through its parameter. If its parameter is named s, use s.firstName, not the outer student.firstName; that lets the function work with other records.

Calculate an average

Add the scores with a loop or the array method reduce(), then divide by the array's length. For a nonempty array, values.reduce((total, value) => total + value, 0) starts at zero and adds each value. To round a number n to one decimal place, use Math.round(n * 10) / 10. Math.round() returns the nearest integer; multiplying first moves the tenths digit into the rounding position.

Object.entries(record) is another built-in method. It returns an array of [key, value] pairs, useful when you need to visit every property. You will use it in the shift audit later in this section.

Edit script.js. The name check also tries Ari Chen, and the average check tries a different score list. Run after each change, then Submit. Scores in this exercise are always nonempty numeric arrays.

Your Task

  1. Destructure firstName and lastName from student.
  2. Complete fullName(s) to return the given record's first and last names separated by a space.
  3. Complete averageScore(s) to return the mean of its scores, rounded to one decimal place.
  4. Log Jordan Lee — avg: 87.8 on a separate line using both helpers and student.
Hint 1
1 / 2
HINT 1

Destructure the two name properties from student.

HINT 2

Add the scores, divide by their count, and round the result to one decimal.

Files
  • ⋄script.js
Loading editor…
Press Run to execute your code.