// Step 1: Scrape GDP data from Wikipedia const wikipediaUrl = "https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)"; const response = await fetch(wikipediaUrl); const html = await response.text(); const parser = new DOMParser(); const doc = parser.parseFromString(html, "text/html"); // Find the table containing GDP data const gdpTable = doc.querySelector(".wikipedia"); const rows = Array.from(gdpTable.querySelectorAll("tr")).slice(1); // Skip the header row // Create an object to store country GDPs const countryGdp = {}; rows.forEach((row) => { const columns = row.querySelectorAll("td"); const countryName = columns[1].textContent.trim(); const gdpValue = parseFloat(columns[2].textContent.replace(/,/g, "").replace("$", "")); countryGdp[countryName] = gdpValue; }); // Step 2: Scrape billionaires' wealth data from Forbes const forbesUrl = "https://www.forbes.com/real-time-billionaires/#72cc12ac3d78"; const forbesResponse = await fetch(forbesUrl); const forbesHtml = await forbesResponse.text(); const forbesDoc = parser.parseFromString(forbesHtml, "text/html"); // Find the table containing billionaire data const billionaireTable = forbesDoc.querySelector(".rtb-table"); const billionaireRows = Array.from(billionaireTable.querySelectorAll("tr")).slice(1); // Skip the header row // Create an array to store billionaire wealth const billionaireWealth = billionaireRows.map((row) => { const columns = row.querySelectorAll("td"); const wealthStr = columns[2].textContent.trim().replace("$", "").replace("B", ""); return parseFloat(wealthStr); }); // Step 3: Calculate the comparison const totalBillionaireWealth = billionaireWealth.reduce((sum, wealth) => sum + wealth, 0); const countriesNeeded = Object.keys(countryGdp).filter((country) => countryGdp[country] < totalBillionaireWealth).length; const gdpSum = Object.values(countryGdp).reduce((sum, gdp) => sum + gdp, 0); console.log("Total billionaire wealth:", totalBillionaireWealth); console.log("Countries needed to match billionaire wealth:", countriesNeeded); console.log("Sum of GDPs:", gdpSum);
top of page

There’s Nothing Here...

We can’t find the page you’re looking for.
Check the URL, or head back home.

bottom of page