How to understand what needs to be done here?

Use destructuring assignment within the argument to the function half to send only max and min inside the function.

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};

// Only change code below this line

const half = () => stats.max + stats.min / 2.0; 
// Only change code above this line

What needs to be done here? From the example it is not clear why this destructuring is needed, what it does, I have never seen this in example projects, you will never understand them on the site itself.

An object like stats not only benefits grouping related data but also enhances code readability, maintainability, reusability, and scalability.

A little better way → const half = () => (stats.max + stats.min) / 2.0;

When they tell me that this improves the readability of the code, it gives me nothing, I didn’t understand it and still don’t understand it

An example if you had a database it would be tiresome to assign all those variable every time a the data is read in.

An example -

        this.database_data = {
            'category': 'images',
            'current_page': this.current_page,
            'per_page': this.per_page,
            'total_count': 0,
            'offset': this.offset
        };

data being read in →

    // Update the UI with the received pagination data
    async paginationUISuccess(parsedData) {
        this.resetLinks();

        this.database_data.offset = await parsedData.offset;
        this.total_pages = Math.ceil(this.database_data.total_count / this.database_data.per_page);

        /* Create the Display Links and add an event listener */
        this.pages = [{}];
        /*
         * Creating the array of page object(s)
         */
        for (let x = 0; x < this.total_pages; x++) {
            this.pages[x] = {page: x + 1};
        }

        this.pages.forEach(link_page => {
            const links = document.createElement('div');
            links.className = 'links';
            this.sidebar.appendChild(links);
            /*
             * Add event listener for the links
             */
            links.addEventListener('click', () => {
                this.database_data.current_page = link_page.page;
                this.createRequest('portfolioPagination.php', this.paginationUISuccess, this.paginationUIError);
            });

            const pageText = document.createElement('p');
            pageText.className = 'linkStyle';
            pageText.id = 'page_' + link_page.page;
            pageText.textContent = link_page.page;
            links.appendChild(pageText);
            if (this.database_data.current_page === link_page.page) {
                links.style.backgroundColor = "#00b28d";
            }
        })

        await this.createImageRequest('portfolioGetImages.php', this.categoryUISuccess, this.categoryUIError);
    }

I will never understand your code, what you wrote there))

There is a whole page on MDN that goes into detail on Destructuring assignments. In essence, it’s a way to extract specific properties from an object. For example:

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};
const {min, max} = stats;

Looking at the MDN page shows you can apparently define function parameters this way also, which is something I was unaware of and rarely see.

const half = ({max, min}) => max + min / 2.0; 
console.log(half(stats));
3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.