Interactive online courses HTML Academy
2026-03-09 12:56 Diff

It turned out that we guessed it right with the classes! But Muffin is never satisfied. Now that he can load products from 1-Muffin, he wants us to update all products on the page. We’ll have to create a more complex program:

Meow! There is an online store with a ready-made layout. We need to show the current information about the products on the interface: special offers and availability in the warehouse.

The data comes in the form of an array of catalogData objects. Each object corresponds to one product and contains properties isAvailable (whether the product is in or out of stock) and isSpecial (whether the product is on special offer or not).

There is a corresponding class for each state of the product:

  • product--available for products in stock;
  • product--unavailable corresponds to a product that is not available;
  • product--special for special offers.

I’m waiting for the result! Don’t be late, the clock is ticking!

Each element of the data array corresponds to a certain product on the page. We already know how to look for DOM elements and could find products one by one using querySelector. For example, using the loop:

for (…) { var product = querySelector('.product:nth-child(' + i + ')'); }

The querySelector method is arranged so that it always returns only the first element found. Therefore, at each iteration, we would have to run a search for the next element in the DOM tree. This search is quite an expensive operation.

It’s much better to get a list of all the elements before the loop, and go through this list inside the loop. Here we can use the querySelectorAll method, which returns not the first element found, but a list (collection) of all elements that match the selector.

Create the updateCards function, in which we will work on the task. Find all products on the page and make sure that the search works correctly.