Skip to content

Commit c40407b

Browse files
committed
challenge 17 readme + root readme updated
1 parent b56d347 commit c40407b

File tree

4 files changed

+61
-67
lines changed

4 files changed

+61
-67
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Exercise 17: Sort Without Articles
2+
Nitish Dayal, Software & Applications Developer - [Contact](http://nitishdayal.me)
3+
Last Commit Date: Dec 23, 2016
4+
5+
We're given an HTML page with an _unordered list_, and an _array of string
6+
values_ in the `script` tag. Sort the values in the array **excluding
7+
the prefixes 'The', A', or 'An'** and place the values into the _unordered
8+
list_ as _list items_.
9+
10+
## Guide
11+
12+
Declare a `const` variable and define it as a _regular expression pattern_
13+
that will match the prefixes we want to exclude that is **case insensitive**.
14+
Create a function that will accept a string value as a parameter and
15+
and returns the string after replacing any parts of the string that match
16+
the previously defined _regular expression pattern_ with an empty string,
17+
excluding leading or trailing whitespaces. Declare a `const` and define
18+
it as the result of sorting through the provided array, passing each
19+
item in the array to the previously defined function. FInally, target
20+
the _unordered list_ and update its _inner HTML_ to display each item
21+
in the array as a _list item_.
22+
23+
**Steps:**
24+
25+
1. Declare a `const` variable and define as a new Regular Expression object.
26+
27+
```JavaScript
28+
const namesPrefix = new RegExp('^(a |the |an )', 'i')
29+
```
30+
31+
2. Declare a `const` and define it as an _arrow function_ which accepts
32+
a parameter `bandName` and returns the provided argument after replacing
33+
any values that match the previously defined RegEx pattern with an empty
34+
string and removing any leading or trailing whitespace.
35+
36+
```JavaScript
37+
const stripPrefixes = (bandName) => bandName.replace(namesPrefixes, '').trim()
38+
```
39+
40+
3. Declare a `const` and define it as the **result** of sorting through the `bands`
41+
array, passing each item into the `stripPrefixes` function to remove prefixes (if
42+
they exist) before comparing them.
43+
44+
```JavaScript
45+
const sortedBands = bands.sort((a, b) => stripPrefixes(a) > stripPrefixes(b) ? 1 : -1)
46+
```
47+
48+
4. Select the `#bands` unordered list and update the _inner HTML_ to be the items in
49+
the sortedBands array stored within _list items_.
50+
51+
```JavaScript
52+
document.querySelector('#bands).innerHTML =
53+
sortedBands
54+
.map(band => `${<li>${band}</li>`})
55+
.join('')
56+
```
57+
58+
Another one down, another one down, another one bites the dust! HEY!

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

Lines changed: 0 additions & 64 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@
5757
// accepts a 'bandName' property and returns that provided argument
5858
// after replacing values that match the previously defined
5959
// regex pattern with an empty string and removing whitespace on either end
60-
const stripArticles = (bandName) => bands[0].replace(namePrefixes, '').trim()
60+
const stripPrefixes = (bandName) => bandName.replace(namePrefixes, '').trim()
6161

6262
// Declare constant variable and define as the result of sorting through the 'bands'
6363
// array depending on the band name excluding prefixes ('A', 'The', 'An')
64-
const sortedBands = bands.sort((a, b) => stripArticles(a) > stripArticles(b) ? 1 : -1);
64+
const sortedBands = bands.sort((a, b) => stripPrefixes(a) > stripPrefixes(b) ? 1 : -1);
6565

6666
// Select the #bands unordered list and update the inner html
6767
// to be the values in the sortedBands array stored within

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Completed written guides can be found in the corresponding challenge directory (
4444
14. [x] ~~[JavaScript References vs. Copying](./exercises/14\ -\ JavaScript\ References\ VS\ Copying)~~
4545
15. [x] ~~[LocalStorage](./exercises/15\ -\ LocalStorage/)~~
4646
16. [x] ~~[Mouse Move Shadow](./exercises/16\ -\ Mouse\ Move\ Shadow/)~~
47-
17. [ ] Sort Without Articles
47+
17. [x] ~~[Sort Without Articles](./exercises/17\ -\ Sort\ Without\ Articles/)~~
4848
18. [ ] Adding Up Times with Reduce
4949
19. [ ] Webcam Fun
5050
20. [ ] Speech Detection

0 commit comments

Comments
 (0)