From e8a177be8c77fe6d657ed74e7e6123e4668c8257 Mon Sep 17 00:00:00 2001 From: Honza Javorek Date: Mon, 25 Aug 2025 20:57:28 +0200 Subject: [PATCH] style: use dollar variables (locating elements) --- .../06_locating_elements.md | 54 +++++++++---------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/sources/academy/webscraping/scraping_basics_javascript2/06_locating_elements.md b/sources/academy/webscraping/scraping_basics_javascript2/06_locating_elements.md index 872e645d30..a647ceaba8 100644 --- a/sources/academy/webscraping/scraping_basics_javascript2/06_locating_elements.md +++ b/sources/academy/webscraping/scraping_basics_javascript2/06_locating_elements.md @@ -80,15 +80,15 @@ if (response.ok) { const $ = cheerio.load(html); $(".product-item").each((i, element) => { - const productItem = $(element); + const $productItem = $(element); - const title = productItem.find(".product-item__title"); - const titleText = title.text(); + const $title = $productItem.find(".product-item__title"); + const title = $title.text(); - const price = productItem.find(".price"); - const priceText = price.text(); + const $price = $productItem.find(".price"); + const price = $price.text(); - console.log(`${titleText} | ${priceText}`); + console.log(`${title} | ${price}`); }); } else { throw new Error(`HTTP ${response.status}`); @@ -170,16 +170,16 @@ if (response.ok) { const $ = cheerio.load(html); $(".product-item").each((i, element) => { - const productItem = $(element); + const $productItem = $(element); - const title = productItem.find(".product-item__title"); - const titleText = title.text(); + const $title = $productItem.find(".product-item__title"); + const title = $title.text(); // highlight-next-line - const price = productItem.find(".price").contents().last(); - const priceText = price.text(); + const $price = $productItem.find(".price").contents().last(); + const price = $price.text(); - console.log(`${titleText} | ${priceText}`); + console.log(`${title} | ${price}`); }); } else { throw new Error(`HTTP ${response.status}`); @@ -243,18 +243,17 @@ Djibouti const $ = cheerio.load(html); $(".wikitable").each((i, tableElement) => { - const table = $(tableElement); - const rows = table.find("tr"); - - rows.each((j, rowElement) => { - const row = $(rowElement); - const cells = row.find("td"); - - if (cells.length > 0) { - const thirdColumn = $(cells[2]); - const link = thirdColumn.find("a").first(); - const linkText = link.text(); - console.log(linkText); + const $table = $(tableElement); + const $rows = $table.find("tr"); + + $rows.each((j, rowElement) => { + const $row = $(rowElement); + const $cells = $row.find("td"); + + if ($cells.length > 0) { + const $thirdColumn = $($cells[2]); + const $link = $thirdColumn.find("a").first(); + console.log($link.text()); } }); }); @@ -289,10 +288,9 @@ Simplify the code from previous exercise. Use a single for loop and a single CSS const $ = cheerio.load(html); $(".wikitable tr td:nth-child(3)").each((i, element) => { - const nameCell = $(element); - const link = nameCell.find("a").first(); - const linkText = link.text(); - console.log(linkText); + const $nameCell = $(element); + const $link = $nameCell.find("a").first(); + console.log($link.text()); }); } else { throw new Error(`HTTP ${response.status}`);