Skip to content

Commit

Permalink
Create Montreal_Price_Stripper.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian-Samuel authored Aug 5, 2018
1 parent b2678ef commit 553b76d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Montreal_Price_Stripper.js
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));
}
});

0 comments on commit 553b76d

Please sign in to comment.