-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenges-12.test.js
208 lines (163 loc) · 8.99 KB
/
challenges-12.test.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict';
/* ------------------------------------------------------------------------------------------------
CHALLENGE 1 - Review
Write a function named replaceZeros that, given a string, uses Regex to replace all '0' with the word 'zero'.
------------------------------------------------------------------------------------------------ */
const replaceZeros = (string) => string.replace(/0/ig, 'zero');
/* ------------------------------------------------------------------------------------------------
CHALLENGE 2
Write a function named validatePin that uses a regular expression pattern to validate a PIN.
If the PIN is four numerical digits long, return true. Otherwise, return false.
------------------------------------------------------------------------------------------------ */
const validatePin = (pin) => /^[0-9]{4}$/g.test(pin);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 3
Write a function named validateWord that uses a regular expression pattern to validate that a word is between 5 and 10 characters long.
If the word is between 5 and 10 characters long, return true. Otherwise, return false.
------------------------------------------------------------------------------------------------ */
const validateWord = (word) => /^[A-Za-z]{5,10}$/g.test(word);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 4
Write a function named hasNumber that uses a regular expression pattern to determine if a string has one or more letter followed by one or more digit.
If it does, return true. If not, return false.
------------------------------------------------------------------------------------------------ */
const hasNumber = (string) => /[A-Za-z]+?\d/g.test(string);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 5
Write a function named validateEmail that takes in an email address and validates it based
on several rules:
- one word, or two words separated by a period, before the @ symbol
- can contain numbers
- can have any of the following top-level domains: .net, .com, or .org
- no other special characters
- no subdomains, ports, etc: must be of the form [email protected], not [email protected]:3000
Return either true or false.
Note: if you ever need to validate an email using a regex in practice, the Internet has the actual regex you should use. It's many many lines long.
------------------------------------------------------------------------------------------------ */
const validateEmail = (email) => /^[A-Za-z0-9]+[.]?[A-Za-z0-9]+@\S+\.(net|com|org)+$/g.test(email);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 6
Write a function named validatePhoneNumber that accepts a phone number and determines if it is valid.
Acceptable formats include:
- (555) 555-5555
- (555)555 5555
- 555 555-5555
- 555-5555555
- 555-555 5555
- 555-555-5555
- 555 555 5555
- 555555-5555
- 5555555555
Your function should include a single regular expression pattern that matches any of these formats.
Return either true or false.
------------------------------------------------------------------------------------------------ */
const validatePhoneNumber = (phoneNumber) => /^\d{3}[ -]?\d{3}[ -]?\d{4}$|^[(]\d{3}[)][ -]?\d{3}[ -]?\d{4}$/.test(phoneNumber);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 7 - Stretch Goal
Write a function named findTagNames that iterates over an array of HTML strings and uses a regular expression pattern to return the closing tags.
For example, findTagNames(['<h1>Hello, world!</h1>', '<p>Welcome to my site</p>']) returns ['/h1', '/p'].
findTagNames(['<div><h1>Hello, world!</h1></div>', '<p>Welcome to my site</p>']) returns ['/h1', '/div', '/p'].
------------------------------------------------------------------------------------------------ */
const findTagNames = elements => elements.map(element => element.match(/\/\w*/g)).reduce((a, b) => a.concat(b), []);
/* ------------------------------------------------------------------------------------------------
TESTS
All the code below will verify that your functions are working to solve the challenges.
DO NOT CHANGE any of the below code.
Run your tests from the console: jest solutions-11.test.js
------------------------------------------------------------------------------------------------ */
describe('Testing challenge 1', () => {
test('It should replace all the 0\'s with the word "zero"', () => {
expect(replaceZeros('301 is s0 much fun!')).toStrictEqual('3zero1 is szero much fun!');
});
});
describe('Testing challenge 2', () => {
test('It should validate a PIN of exactly four digits', () => {
expect(validatePin(1234)).toBeTruthy();
expect(validatePin(123)).toBeFalsy();
expect(validatePin(12345)).toBeFalsy();
expect(validatePin('abcd')).toBeFalsy();
expect(validatePin('7890')).toBeTruthy();
expect(validatePin('0789')).toBeTruthy();
expect(validatePin(789)).toBeFalsy();
expect(validatePin('0000')).toBeTruthy();
});
});
describe('Testing challenge 3', () => {
test('It should validate a word between 5 and 10 characters', () => {
expect(validateWord('Hello')).toBeTruthy();
expect(validateWord('Bob')).toBeFalsy();
expect(validateWord(12345)).toBeFalsy();
expect(validateWord('abcdefghijkl')).toBeFalsy();
expect(validateWord('cookie')).toBeTruthy();
expect(validateWord(789)).toBeFalsy();
expect(validateWord('Code301')).toBeFalsy();
});
});
describe('Testing challenge 4', () => {
test('It should return true if a string has one or more word characters followed by one or more digits', () => {
expect(hasNumber('Hell0')).toBeTruthy();
expect(hasNumber('Bob')).toBeFalsy();
expect(hasNumber(12345)).toBeFalsy();
expect(hasNumber('abcdefghijkl')).toBeFalsy();
expect(hasNumber('c00kie')).toBeTruthy();
expect(hasNumber(789)).toBeFalsy();
expect(hasNumber('Code301')).toBeTruthy();
expect(hasNumber('99Code')).toBeFalsy();
});
});
describe('Testing challenge 5', () => {
test('It should match a basic email', () => {
expect(validateEmail('[email protected]')).toBeTruthy();
});
test('It should match if the email contains a period', () => {
expect(validateEmail('[email protected]')).toBeTruthy();
});
test('It should match if the email contains other top-level domains', () => {
expect(validateEmail('[email protected]')).toBeTruthy();
});
test('It should match if the email contains a period and other top-level domains', () => {
expect(validateEmail('[email protected]')).toBeTruthy();
});
test('It should fail things that aren\'t email addresses', () => {
expect(validateEmail('justastring')).toBeFalsy();
expect(validateEmail('missing@adomain')).toBeFalsy();
expect(validateEmail('@noname.com')).toBeFalsy();
expect(validateEmail('[email protected]')).toBeFalsy();
expect(validateEmail('[email protected]')).toBeFalsy();
expect(validateEmail('[email protected]')).toBeFalsy();
expect(validateEmail('missing.atsymbol.net')).toBeFalsy();
expect(validateEmail('[email protected]')).toBeFalsy();
expect(validateEmail('[email protected]')).toBeFalsy();
});
});
describe('Testing challenge 6', () => {
test('It should match the acceptable phone number formats', () => {
expect(validatePhoneNumber('(555) 555-5555')).toBeTruthy();
expect(validatePhoneNumber('555 555-5555')).toBeTruthy();
expect(validatePhoneNumber('555-555-5555')).toBeTruthy();
expect(validatePhoneNumber('555 5555555')).toBeTruthy();
expect(validatePhoneNumber('5555555555')).toBeTruthy();
expect(validatePhoneNumber('234 567 8910')).toBeTruthy();
});
test('It should not match unacceptable phone number formats', () => {
expect(validatePhoneNumber('abcdefghij')).toBeFalsy();
expect(validatePhoneNumber('222 222 2222 ext. 2222')).toBeFalsy();
expect(validatePhoneNumber('(222 222-2222')).toBeFalsy();
expect(validatePhoneNumber('222 222-2222-')).toBeFalsy();
expect(validatePhoneNumber('(222 222- 2222')).toBeFalsy();
expect(validatePhoneNumber('(222 222 -2222')).toBeFalsy();
expect(validatePhoneNumber('523 555--5555')).toBeFalsy();
expect(validatePhoneNumber('55555555555')).toBeFalsy();
expect(validatePhoneNumber('55555555555')).toBeFalsy();
expect(validatePhoneNumber('55555555555')).toBeFalsy();
expect(validatePhoneNumber('55_55_5555')).toBeFalsy();
});
});
describe('Testing challenge 7', () => {
test('It should return the closing tags', () => {
expect(findTagNames(['<h1>Hello, world!</h1>', '<p>Welcome to my site</p>'])).toStrictEqual(['/h1', '/p']);
});
test('It should work if there are multiple closing tags in a single string', () => {
expect(findTagNames(['<div><h1>Hello, world!</h1></div>', '<p>Welcome to my site</p>'])).toStrictEqual(['/h1', '/div', '/p']);
});
});