Skip to content

Commit e3d3de7

Browse files
committed
Add pre-video attempt at 17 - Sort Without Articles
1 parent 41bcbcd commit e3d3de7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

17 - Sort Without Articles/my-index.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,37 @@
4545
<script>
4646
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'];
4747

48+
const articles = ['The', 'A', 'An']
49+
50+
function getFirstWord(phrase) {
51+
return phrase.split(' ')[0]
52+
}
53+
54+
function startsWithArticle(phrase) {
55+
const firstWord = getFirstWord(phrase);
56+
return articles.indexOf(firstWord) === -1 ? false : true;
57+
}
58+
59+
function trimArticle(phrase) {
60+
if (startsWithArticle(phrase)) {
61+
return phrase.split(' ').slice(1).join(' ')
62+
}
63+
return phrase
64+
}
65+
66+
function compare(a, b) {
67+
const trimmedA = trimArticle(a);
68+
const trimmedB = trimArticle(b);
69+
if (trimmedA > trimmedB) {
70+
return 1;
71+
}
72+
if (trimmedA < trimmedB) {
73+
return -1;
74+
}
75+
return 0;
76+
}
77+
78+
bands.sort(compare);
4879

4980
</script>
5081

0 commit comments

Comments
 (0)