Skip to content

Commit dfceb6b

Browse files
c0d1ng-ma5tergikf
andauthored
fix(curriculum): prevent hardcoding phone number validator (freeCodeCamp#58360)
Co-authored-by: Krzysztof G. <[email protected]>
1 parent 4e39195 commit dfceb6b

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

curriculum/challenges/english/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/telephone-number-validator.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,52 @@ assert(telephoneCheck('55 55-55-555-5') === false);
192192
assert(telephoneCheck('11 555-555-5555') === false);
193193
```
194194

195+
`telephoneCheck()`, when called with any valid number, should return `true`.
196+
197+
```js
198+
199+
const validPatterns = [
200+
'1 XXX-XXX-XXXX',
201+
'1 (XXX)XXX-XXXX',
202+
'1(XXX)XXX-XXXX',
203+
'1 XXX XXX XXXX',
204+
'XXXXXXXXXX',
205+
'XXX-XXX-XXXX',
206+
'(XXX)XXX-XXXX',
207+
];
208+
209+
validPatterns.forEach(pattern => {
210+
while (pattern.includes('X')) {
211+
pattern = pattern.replace('X', Math.floor(Math.random() * 7) + 2); //While this may seem weird at first, it's required for the CI build to pass
212+
//This is apparently because the solution provided for CI purposes actually checks for valid area and exchange codes.
213+
}
214+
assert.isTrue(telephoneCheck(pattern));
215+
});
216+
```
217+
218+
`telephoneCheck()`, when called with an invalid number, should return `false`.
219+
220+
```js
221+
222+
const invalidPatterns = [
223+
'10 XXX-XXX-XXXX',
224+
'1 (XX)XXX-XXXX',
225+
'1!(XXX)XXX-XXXX',
226+
'-1 XXX XXX XXXX',
227+
'XXXXXXXX',
228+
'XXX#XXX-XXXX',
229+
'(XXXXXX-XXXX',
230+
];
231+
232+
invalidPatterns.forEach(pattern => {
233+
while (pattern.includes('X')) {
234+
pattern = pattern.replace('X', Math.floor(Math.random() * 10));
235+
}
236+
assert.isFalse(telephoneCheck(pattern));
237+
});
238+
```
239+
240+
195241
# --seed--
196242

197243
## --seed-contents--

curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-telephone-number-validator-project/build-a-telephone-number-validator.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Note that the area code is required. Also, if the country code is provided, you
6060
1. When `#user-input` contains `(555)5(55?)-5555` and `#check-btn` is clicked, `#results-div` should contain the text `"Invalid US number: (555)5(55?)-5555"`.
6161
1. When the `#user-input` element contains `55 55-55-555-5` and the `#check-btn` element is clicked, the `#results-div` element should contain the text `"Invalid US number: 55 55-55-555-5"`.
6262
1. When the `#user-input` element contains `11 555-555-5555` and the `#check-btn` element is clicked, the `#results-div` element should contain the text `"Invalid US number: 11 555-555-5555"`.
63+
1. When the `#user-input` element contains a valid US number and the `#check-btn` element is clicked, the `#results-div` element should contain the text `"Valid US number: "` followed by the number.
64+
1. When the `#user-input` element contains an invalid US number and the `#check-btn` element is clicked, the `#results-div` element should contain the text `"Invalid US number: "` followed by the number.
6365

6466
Fulfill the user stories and pass all the tests below to complete this project. Give it your own personal style. Happy Coding!
6567

@@ -510,6 +512,59 @@ checkBtn.click();
510512
assert.strictEqual(resultsDiv.innerText.trim().toLowerCase(), 'invalid us number: 11 555-555-5555');
511513
```
512514
515+
When the `#user-input` element contains a valid US number and the `#check-btn` element is clicked, the `#results-div` element should contain the text `"Valid US number: "` followed by the number.
516+
517+
```js
518+
519+
const validPatterns = [
520+
'1 XXX-XXX-XXXX',
521+
'1 (XXX)XXX-XXXX',
522+
'1(XXX)XXX-XXXX',
523+
'1 XXX XXX XXXX',
524+
'XXXXXXXXXX',
525+
'XXX-XXX-XXXX',
526+
'(XXX)XXX-XXXX',
527+
];
528+
529+
validPatterns.forEach(pattern => {
530+
while (pattern.includes('X')) {
531+
pattern = pattern.replace('X', Math.floor(Math.random() * 7) + 2); //While this may seem weird at first, it's required for the CI build to pass
532+
//This is apparently because the solution provided for CI purposes actually checks for valid area and exchange codes.
533+
}
534+
resultsDiv.innerHTML = '';
535+
userInput.value = pattern;
536+
userInput.dispatchEvent(new Event('change'));
537+
checkBtn.click();
538+
assert.strictEqual(document.getElementById('results-div').innerText.trim().toLowerCase(), `valid us number: ${pattern}`);
539+
});
540+
```
541+
542+
When the `#user-input` element contains an invalid US number and the `#check-btn` element is clicked, the `#results-div` element should contain the text `"Invalid US number: "` followed by the number.
543+
544+
```js
545+
546+
const invalidPatterns = [
547+
'10 XXX-XXX-XXXX',
548+
'1 (XX)XXX-XXXX',
549+
'1!(XXX)XXX-XXXX',
550+
'-1 XXX XXX XXXX',
551+
'XXXXXXXX',
552+
'XXX#XXX-XXXX',
553+
'(XXXXXX-XXXX',
554+
];
555+
556+
invalidPatterns.forEach(pattern => {
557+
while (pattern.includes('X')) {
558+
pattern = pattern.replace('X', Math.floor(Math.random() * 10));
559+
}
560+
resultsDiv.innerHTML = '';
561+
userInput.value = pattern;
562+
userInput.dispatchEvent(new Event('change'));
563+
checkBtn.click();
564+
assert.strictEqual(document.getElementById('results-div').innerText.trim().toLowerCase(), `invalid us number: ${pattern}`);
565+
});
566+
```
567+
513568
# --seed--
514569
515570
## --seed-contents--

0 commit comments

Comments
 (0)