Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d51e9a9

Browse files
Merge pull request #20 from sahilatahar/main
[Added]: Four New Challenges
2 parents 35d5f7b + cf0547b commit d51e9a9

File tree

9 files changed

+179
-2
lines changed

9 files changed

+179
-2
lines changed

‎README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,54 @@ Write a function called `celsiusToFahrenheit` that takes a temperature in Celsiu
154154

155155
[Solution Explanation](./solutions/ch_16_Celsius_To_Fahrenheit/readme.md)
156156

157-
## Challenge 17: Calculate the Sum of N Natural Number.
157+
## Challenge 17: Calculate the Sum of N Natural Number
158158

159159
Write a function that takes a number as input and returns sum from 1 to `n`. For example, if the input is 10, the function should return 55.
160160

161161
Write a function called `getNaturalSum` that takes a number `n` as its parameter and returns sum of natural number.
162162

163163
[Solution Explanation](./solutions/ch_17_Sum_Of_N_Natural_Number/readme.md)
164164

165-
## Challenge 18: Convert Decimal to Binary.
165+
## Challenge 18: Convert Decimal to Binary
166166

167167
Write a function that takes a decimal number as input and returns binary string. For example, if the input is 12, the function should return 1100.
168168

169169
Write a function called `decimalToBinary` that takes a decimal number as `decimal` in its parameter and returns binary string.
170170

171171
[Solution Explanation](./solutions/ch_18_Decimal_To_Binary/readme.md)
172172

173+
## Challenge 19: Count Vowels in a String
174+
175+
Write a function that takes a string as input and returns number of vowels in string. For example, if the string is "Hello World!", the function should return 3.
176+
177+
Write a function called `countVowels` that takes a string as `str` in its parameter and returns number of vowels in string.
178+
179+
[Solution Explanation](./solutions/ch_19_Count_Vowels_In_String/readme.md)
180+
181+
## Challenge 20: Check String Url
182+
183+
Write a function that takes a url string as input and returns true if url is valid and false otherwise. For example, if the string is "https://www.example.com", the function should return true.
184+
185+
Write a function called `isValidURL` that takes a string url as `url` in its parameter and returns true or false.
186+
187+
[Solution Explanation](./solutions/ch_20_Check_String_Url/readme.md)
188+
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+
173205
<!-- Add new challenges before this comment -->
174206

175207
## Contributors
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Function to count vowels in the string
2+
function countVowels(str) {
3+
4+
str = str.toLowerCase(); // Convert string to lower case
5+
6+
const vowels = ['a', 'e', 'i', 'o', 'u']; // List of vowels
7+
let count = 0; // Declare variable to count number of vowels
8+
9+
for (let i = 0; i < str.length; i++) {
10+
// Checking if vowels include the character at str[i]
11+
if (vowels.includes(str[i])) {
12+
count++;
13+
}
14+
}
15+
16+
return count; // Return the total number of vowels
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Challenge 19: Count Vowels in a String
2+
3+
Write a function that takes a string as input and returns number of vowels in string. For example, if the string is "Hello World!", the function should return 3.
4+
5+
Write a function called `countVowels` that takes a string as `str` in its parameter and returns number of vowels in string.
6+
7+
## Answer
8+
9+
```javascript
10+
// Function to count vowels in the string
11+
function countVowels(str) {
12+
13+
str = str.toLowerCase(); // Convert string to lower case
14+
15+
const vowels = ['a', 'e', 'i', 'o', 'u']; // List of vowels
16+
let count = 0; // Declare variable to count number of vowels
17+
18+
for (let i = 0; i < str.length; i++) {
19+
// Checking if vowels include the character at str[i]
20+
if (vowels.includes(str[i])) {
21+
count++;
22+
}
23+
}
24+
25+
return count; // Return the total number of vowels
26+
}
27+
```
28+
29+
## Answer Explanation
30+
31+
In the `countVowels` function, first convert `str` string to lowercase. Declare `count` variable to store number of vowels in `str` and declare `vowels` array of vowels and run a loop for every character in `str`. Inside the loop check if str[index] contains vowels character if yes increase count variable by one. After the loop finished return `count`, total number of vowels.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Function to check string url
2+
function isValidURL(url) {
3+
// Regular expression pattern for URL validation
4+
const urlPattern = /^(https?:\/\/)?[\w\-]+(\.[\w\-]+)+[/#?]?.*$/i;
5+
6+
// Test if the string matches the URL pattern
7+
return urlPattern.test(url);
8+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Challenge 20: Check String Url
2+
3+
Write a function that takes a url string as input and returns true if url is valid and false otherwise. For example, if the string is "https://www.example.com", the function should return true.
4+
5+
Write a function called `isValidURL` that takes a string url as `url` in its parameter and returns true or false.
6+
7+
## Answer
8+
9+
```javascript
10+
// Function to check string url
11+
function isValidURL(url) {
12+
// Regular expression pattern for URL validation
13+
const urlPattern = /^(https?:\/\/)?[\w\-]+(\.[\w\-]+)+[/#?]?.*$/i;
14+
15+
// Test if the string matches the URL pattern
16+
return urlPattern.test(url);
17+
}
18+
```
19+
20+
## Answer Explanation
21+
22+
The `isValidURL` function takes a string as input and uses a regular expression to check if it's a valid URL. The regular expression `^(https?:\/\/)?[\w\-]+(\.[\w\-]+)+[/#?]?.*$` is used to match the URL pattern.
23+
24+
The test method is then used to check if the input string matches the regular expression, if it's a valid URL function returns true otherwise false.
Lines changed: 8 additions & 0 deletions
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+
}
Lines changed: 24 additions & 0 deletions
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.
Lines changed: 10 additions & 0 deletions
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+
Lines changed: 23 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /