Fix array splice
This commit is contained in:
parent
1b70ac612e
commit
fecb114063
2 changed files with 14 additions and 10 deletions
22
index.js
22
index.js
|
@ -11,31 +11,35 @@ async function sortHackerNewsArticles() {
|
||||||
await page.goto("https://news.ycombinator.com/newest");
|
await page.goto("https://news.ycombinator.com/newest");
|
||||||
|
|
||||||
// instantiate array for all date objects
|
// instantiate array for all date objects
|
||||||
let datestamps = []
|
let datestamps = [];
|
||||||
|
|
||||||
// loop through process per page
|
// loop through process per page
|
||||||
for (var pageview = 0; pageview <= 3; pageview++) {
|
for (var pageview = 0; pageview <= 3; pageview++) {
|
||||||
// locate each article's age span
|
// locate each article's age span
|
||||||
let datespans = await page.locator('.age').all();
|
let datespans = await page.locator(".age").all();
|
||||||
|
|
||||||
// iterate through date spans to get timestamps
|
// iterate through date spans to get timestamps
|
||||||
for (row = 0; row < datespans.length; row++) {
|
for (row = 0; row < datespans.length; row++) {
|
||||||
// turn timestamp strings into date objects and push to array
|
// turn timestamp strings into date objects and push to array
|
||||||
datestamps.push(new Date(await datespans[row].getAttribute('title')));
|
datestamps.push(new Date(await datespans[row].getAttribute("title")));
|
||||||
}
|
}
|
||||||
|
|
||||||
// click the next button as needed
|
// click the next button as needed
|
||||||
await page.getByRole('link', { name: 'More', exact: true }).click();
|
await page.getByRole("link", { name: "More", exact: true }).click();
|
||||||
}
|
}
|
||||||
|
|
||||||
// drop extra articles from last page grab
|
// drop extra array elements
|
||||||
datestamps = datestamps.slice(0, 100);
|
datestamps.splice(100, 20);
|
||||||
|
|
||||||
// report results
|
// check if the website dates are sorted and report results
|
||||||
if (datestamps === datestamps.sort()) {
|
if (datestamps === datestamps.sort()) {
|
||||||
console.log("Success! The first 100 articles are correctly sorted by posting time.")
|
console.log(
|
||||||
|
"Success! The first 100 articles are correctly sorted by posting time.",
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
console.log("Failure… The first 100 articles are not correctly sorted by posting time.")
|
console.log(
|
||||||
|
"Failure… The first 100 articles are not correctly sorted by posting time.",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue