-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path17.js
More file actions
50 lines (46 loc) · 1.45 KB
/
17.js
File metadata and controls
50 lines (46 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function sortWithoutArticles() {
function originalData() {
return [
"The Plot in You",
"The Devil Wears Prada",
"Pierce the Veil",
"Norma Jean",
"The Bled",
"Say Anything",
"The Midway State",
"We Came as Romans",
"Counterparts",
"Oh, Sleeper",
"A Skylit Drive",
"Anywhere But Here",
"An Old Dog",
];
}
const button = document.getElementsByClassName("button");
var sortStatus = 0;
function strip(brandName) {
return brandName.replace(/^(a |the |an)/i, "").trim();
}
function sortIt() {
const bands = originalData();
if (sortStatus == 0) {
document.querySelector("#bands").innerHTML = bands
.map((band) => `<li>${band}</li>`)
.join("");
button[0].innerHTML = "Sort It";
sortStatus = 1;
} else {
const sortedBands = bands.sort((a, b) =>
strip(a) > strip(b) ? 1 : -1
);
document.querySelector("#bands").innerHTML = sortedBands
.map((band) => `<li>${band}</li>`)
.join("");
button[0].innerHTML = "Unsort It";
sortStatus = 0;
}
}
sortIt();
button[0].addEventListener("click", sortIt);
}
window.addEventListener("DOMContentLoaded", sortWithoutArticles);