Skip to content

Commit b56d347

Browse files
committed
challenge 17 answer key complete
1 parent f9d2541 commit b56d347

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

exercises/17 - Sort Without Articles/index-START.html

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<title>Sort Without Articles</title>
67
</head>
8+
79
<body>
810

911
<style>
@@ -40,13 +42,37 @@
4042

4143
</style>
4244

43-
<ul id="bands"></ul>
45+
<ul id="bands"></ul>
4446

4547
<script>
46-
const bands = ['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'];
48+
const bands = ['The Plot in You', 'The Devil Wears Prada', 'Pierce the Veil', 'Norma Jean',
49+
'The Bled', 'Say Anything', 'The Midway State', 'We Came as Romans', 'Counterparts',
50+
'Oh, Sleeper', 'A Skylit Drive', 'Anywhere But Here', 'An Old Dog']
51+
52+
53+
// Declare constant variable and define as new Regular Expression object
54+
const namePrefixes = new RegExp('^(a |the |an )', 'i')
4755

56+
// Declare constant variable and define as an arrow function which
57+
// accepts a 'bandName' property and returns that provided argument
58+
// after replacing values that match the previously defined
59+
// regex pattern with an empty string and removing whitespace on either end
60+
const stripArticles = (bandName) => bands[0].replace(namePrefixes, '').trim()
61+
62+
// Declare constant variable and define as the result of sorting through the 'bands'
63+
// array depending on the band name excluding prefixes ('A', 'The', 'An')
64+
const sortedBands = bands.sort((a, b) => stripArticles(a) > stripArticles(b) ? 1 : -1);
65+
66+
// Select the #bands unordered list and update the inner html
67+
// to be the values in the sortedBands array stored within
68+
// list items.
69+
document.querySelector("#bands").innerHTML =
70+
sortedBands
71+
.map(band => `<li>${band}</li>`)
72+
.join('')
4873

4974
</script>
5075

5176
</body>
52-
</html>
77+
78+
</html>

0 commit comments

Comments
 (0)