Code24Lab

You are not signed in β€” the lab works, but your progress is not saved. Sign up

Lesson 1 of 21 is free. Unlock all 21 hands-on labs, the final exam and your certificate.

Buy course β€” €9.90
Read first

The code

// products.js – Fernwood Trail Co. catalog page
$(function () {
  var $items = $('.product');
  var total = 0;
  $items.each(function () {
    total += $(this).data('price');
  });
  console.log($items.length, total, total / $items.length);
  var saleTotal = 0;
  $('.product.sale').each(function () {
    saleTotal += $(this).data('price');
  });
  $('.sale').addClass('highlight');
});
The rows are the page's elements in document order. A selector never changes that order: .each() visits them top to bottom.
Loading the lab…