You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
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
+
functioncountVowels(str) {
12
+
13
+
str =str.toLowerCase(); // Convert string to lower case
14
+
15
+
constvowels= ['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.
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.
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.
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
+
functionisValidUsername(username) {
12
+
// Regular expression pattern for username validation
13
+
constregex=/^[a-zA-Z0-9_]{4,16}$/;
14
+
15
+
// Test if the string matches the username pattern
16
+
returnregex.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.
0 commit comments