Skip to content

Commit 2a8b588

Browse files
committed
Challenge 19 & 20 Added
1 parent 5393a69 commit 2a8b588

File tree

5 files changed

+98
-2
lines changed

5 files changed

+98
-2
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,38 @@ 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+
173189
<!-- Add new challenges before this comment -->
174190

175191
## 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.

0 commit comments

Comments
 (0)