Skip to content

Commit cf0547b

Browse files
committed
Challenge 21 & 22 Added
1 parent ef47439 commit cf0547b

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,22 @@ Write a function called `isValidURL` that takes a string url as `url` in its par
186186

187187
[Solution Explanation](./solutions/ch_20_Check_String_Url/readme.md)
188188

189+
## Challenge 21: Validate Username
190+
191+
Write a function that checks if a given string is a valid username. A valid username should contain only alphanumeric characters and underscores, and should be between 4 and 16 characters long.
192+
193+
Write a function called `isValidUsername` that takes a string `username` in its parameter and returns true or false.
194+
195+
[Solution Explanation](./solutions/ch_21_Validate_Username/readme.md)
196+
197+
## Challenge 22: Check Leap Year
198+
199+
Write a function that checks if a given year is a leap year.
200+
201+
Write a function called `isLeapYear` that takes `year` in its parameter and returns true or false.
202+
203+
[Solution Explanation](./solutions/ch_22_Check_Leap_Year/readme.md)
204+
189205
<!-- Add new challenges before this comment -->
190206

191207
## Contributors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Function to validate username
2+
function isValidUsername(username) {
3+
// Regular expression pattern for username validation
4+
const regex = /^[a-zA-Z0-9_]{4,16}$/;
5+
6+
// Test if the string matches the username pattern
7+
return regex.test(username);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Challenge 21: Validate Username
2+
3+
Write a function that checks if a given string is a valid username. A valid username should contain only alphanumeric characters and underscores, and should be between 4 and 16 characters long.
4+
5+
Write a function called `isValidUsername` that takes a string `username` in its parameter and returns true or false.
6+
7+
## Answer
8+
9+
```javascript
10+
// Function to validate username
11+
function isValidUsername(username) {
12+
// Regular expression pattern for username validation
13+
const regex = /^[a-zA-Z0-9_]{4,16}$/;
14+
15+
// Test if the string matches the username pattern
16+
return regex.test(username);
17+
}
18+
```
19+
20+
## Answer Explanation
21+
22+
The `isValidUsername` function takes a string as input and uses a regular expression to check if it's a valid username. The regular expression `^[a-zA-Z0-9_]{4,16}$` is used to match the username pattern.
23+
24+
The test method is then used to check if the input string matches the regular expression, if it's a valid username function returns true otherwise false.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Function to check leap year
2+
function isLeapYear(year) {
3+
// Gregorian calendar rule to check leap year
4+
if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
5+
return true;
6+
}
7+
8+
return false;
9+
}
10+
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Challenge 22: Check Leap Year
2+
3+
Write a function that checks if a given year is a leap year.
4+
5+
Write a function called `isLeapYear` that takes `year` in its parameter and returns true or false.
6+
7+
## Answer
8+
9+
```javascript
10+
// Function to check leap year
11+
function isLeapYear(year) {
12+
// Gregorian calendar rule to check leap year
13+
if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
14+
return true;
15+
}
16+
17+
return false;
18+
}
19+
```
20+
21+
## Answer Explanation
22+
23+
The `isLeapYear` function takes `year` as input and check condition for leap year. if condition is satisfied returns true otherwise false.

0 commit comments

Comments
 (0)