Skip to content

Commit 813d6e2

Browse files
committed
Place JS code in IIFE
1 parent d94311d commit 813d6e2

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

exercises/17 - Sort Without Articles/index.html

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,33 @@
4545
<ul id="bands"></ul>
4646

4747
<script>
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')
55-
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 stripPrefixes = (bandName) => bandName.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) => stripPrefixes(a) > stripPrefixes(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('')
48+
(() => {
49+
const bands = ['The Plot in You', 'The Devil Wears Prada', 'Pierce the Veil', 'Norma Jean',
50+
'The Bled', 'Say Anything', 'The Midway State', 'We Came as Romans', 'Counterparts',
51+
'Oh, Sleeper', 'A Skylit Drive', 'Anywhere But Here', 'An Old Dog']
52+
53+
54+
// Declare constant variable and define as new Regular Expression object
55+
const namePrefixes = new RegExp('^(a |the |an )', 'i')
56+
57+
// Declare constant variable and define as an arrow function which
58+
// accepts a 'bandName' property and returns that provided argument
59+
// after replacing values that match the previously defined
60+
// regex pattern with an empty string and removing whitespace on either end
61+
const stripPrefixes = (bandName) => bandName.replace(namePrefixes, '').trim()
62+
63+
// Declare constant variable and define as the result of sorting through the 'bands'
64+
// array depending on the band name excluding prefixes ('A', 'The', 'An')
65+
const sortedBands = bands.sort((a, b) => stripPrefixes(a) > stripPrefixes(b) ? 1 : -1);
66+
67+
// Select the #bands unordered list and update the inner html
68+
// to be the values in the sortedBands array stored within
69+
// list items.
70+
document.querySelector("#bands").innerHTML =
71+
sortedBands
72+
.map(band => `<li>${band}</li>`)
73+
.join('')
74+
})();
7375

7476
</script>
7577

0 commit comments

Comments
 (0)