-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b2678ef
commit 553b76d
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* Purpose of this task was to be able to scrape the list of prices onto | ||
the console and sum them. This automates the manual copy and pasting work one would have to do | ||
Website scraped: https://www.mtlblog.com/lifestyle/cost-of-living-montreal | ||
*/ | ||
|
||
// Target all the content on the page | ||
let it = document.querySelectorAll('#article-text *'); | ||
// initialise a string | ||
let words = ""; | ||
// cycle through each piece of content on the page | ||
it.forEach(function(price, i) { | ||
//store the HTML content in a variable | ||
let money = price.innerHTML; | ||
//concat the money HTML into an array called words | ||
words = words + money; | ||
// create a regular expression to match the dollar signs followed with a number i.e. the prices | ||
let patt = /\$\d+/igm; | ||
// store a variable matching the pattern of the regular expression created above | ||
let pricing = words.match(patt); | ||
// if the iteration of the array matches the node list length - 1 | ||
if (i == it.length -1) { | ||
// remove the dollar sign off the array | ||
let freedom = pricing.map(x => x.replace(/\$/igm, "")); | ||
// convert the array into numbers and store it into the variable conv | ||
let conv = freedom.map(Number); | ||
// log out the sum of the values onto the console | ||
console.log(conv.reduce((a,b) => a+b,0)); | ||
} | ||
}); |