Pracrice Resource - JS

Ques 1. Write a function called sumArray that takes an array of numbers as input and returns the sum of all the numbers in the array.

Ques 2. Write a function called validateEmail that takes an email address as input and returns true if the email address is valid, and false otherwise. A valid email address should meet the following criteria:

  1. It should contain exactly one “@” symbol.
  2. It should contain at least one character before the “@” symbol.
  3. It should contain at least one character after the “@” symbol.
  4. It should have a domain name with at least one “.” symbol after the “@” symbol.
  5. The domain name should have at least two characters after the last “.” symbol.

Function signature:
function validateEmail(email) { // Your code here }

Sure! Here’s another JavaScript practice problem for you:

Problem: Write a function called validateEmail that takes an email address as input and returns true if the email address is valid, and false otherwise. A valid email address should meet the following criteria:

  1. It should contain exactly one “@” symbol.
  2. It should contain at least one character before the “@” symbol.
  3. It should contain at least one character after the “@” symbol.
  4. It should have a domain name with at least one “.” symbol after the “@” symbol.
  5. The domain name should have at least two characters after the last “.” symbol.

Function signature:

javascriptCopy code

function validateEmail(email) {
  // Your code here
}

Example usage:

console.log(validateEmail("user@example.com")); // Output: true
console.log(validateEmail("user@.com")); // Output: false
console.log(validateEmail("@example.com")); // Output: false
console.log(validateEmail("user@domain.")); // Output: false
console.log(validateEmail("user@domain.c")); // Output: false

Constraints:

  • The input email address email will be a string with length between 3 and 256 characters.
  • The email address may contain uppercase and lowercase letters, digits, special characters, and “@” and “.” symbols.
  • You can assume that there will be no leading or trailing spaces in the input email address.
  • You can also assume that the “@” and “.” symbols will not appear consecutively in the input email address.
3 Likes

function sumOfNumbersInArray(arr) {
let sum = 0,
i = arr.length - 1;
while (i >= 0) {
sum += arr[i];
arr.pop(arr[i]);
i–;
}
return sum;
}
function validateEmail(str) {
str = str.split(“@”);
if (str.length == 2 && str[0] != “” && str[1] != “”) {
str = str[1].split(“.”);
if (str.length == 2 && str[0] != “” && str[1] == “com”) return true;
return false;
}
return false;
}
console.log(sumOfNumbersInArray([5,10,15,20])); //output :50
console.log(validateEmail(“user@example.com”)); // Output: true
console.log(validateEmail(“user@.com”)); // Output: false
console.log(validateEmail(“@example.com”)); // Output: false
console.log(validateEmail(“user@domain.”)); // Output: false
console.log(validateEmail(“user@domain.c”)); // Output: false

@kashifk9065
Q1-Ans-function sumArray(arr) {
let total = 0;
for (let i = 0; i < arr.length; i++) {
total += arr[i];
}
return total;
}
const arr = [1, 2, 3, 4, 5];
const totalSum = sumArray(arr);
console.log(totalSum);

@kashifk9065
Q2-Ans-function validateEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+.[^\s@]{2,}$/;
return emailRegex.test(email);
}
then we can check the following thing in console
console.log(validateEmail(“john@example.com”));
console.log(validateEmail(“riya@.com”));
console.log(validateEmail(“@example.com”));
console.log(validateEmail(“sam@domain.”));
console.log(validateEmail(“lily@domain.c”));

mam, you have used regular expressions right ? would you please explain how it works.

1 Like

yess i will explain tell me which part you are asking @kamepallilakshmanbha

about the regular expressions, i know nothinf about them it makes easy to validate

1 Like

yess @kamepallilakshmanbha you are right i will explain you how make you understand in an easy way –
Yes, the validateEmail function uses a regular expression to validate the email address.

A regular expression, also known as a regex or regexp, is a sequence of characters that defines a search pattern. It is used to match and manipulate strings, including patterns of characters that appear in text.

In the validateEmail function, the regular expression used to validate the email address is /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.

Here’s what each part of the regular expression means:

  • / - The start of the regular expression.
  • ^ - Matches the start of the string.
  • [^\s@]+ - Matches one or more characters that are not whitespace or the @ symbol.
  • @ - Matches the @ symbol.
  • [^\s@]+ - Matches one or more characters that are not whitespace or the @ symbol.
  • \. - Matches a literal period character.
  • [^\s@]{2,} - Matches two or more characters that are not whitespace or the @ symbol.
  • $ - Matches the end of the string.
  • / - The end of the regular expression.

When the validateEmail function is called with an email address, it tests whether the email address matches this regular expression. If the email address matches the pattern, the function returns true, indicating that the email address is valid. If the email address does not match the pattern, the function returns false, indicating that the email address is invalid.

So in summary, regular expressions are a powerful tool for manipulating and validating strings. They use a combination of special characters and quantifiers to create a pattern that can match and extract information from text. In the case of the validateEmail function, the regular expression is used to ensure that the email address provided is in a valid format.
I hope that what I said has cleared things up for you
Is everything clear to you now? Please let me know if you have any further questions.

1 Like

thank you mam, i will try to use them from now on.

1 Like

Yes, of course. There are many methods available to complete that task, and you can choose whichever method you prefer to get the job done.

1 Like