diff --git a/session-01/validators.js b/session-01/validators.js index 56ea66d..cae4ea7 100644 --- a/session-01/validators.js +++ b/session-01/validators.js @@ -7,7 +7,22 @@ - username cannot contain special characters */ function validUsername(username) { - return; + // Check length + if (username.length < 3 || username.length > 10) { + return false; + } + + // Check if it begins with a letter + if (!/^[a-zA-Z]/.test(username)) { + return false; + } + + // Check if it only contains letters and numbers + if (!/^[a-zA-Z0-9]+$/.test(username)) { + return false; + } + + return true; } /* @@ -17,7 +32,21 @@ function validUsername(username) { - password must contain at least 1 letter, 1 number, and 1 special character */ function validPassword(password) { - return; + // Check length + if (password.length < 10 || password.length > 64) { + return false; + } + + // Check for at least 1 letter, 1 number, and 1 special character + const hasLetter = /[a-zA-Z]/.test(password); + const hasNumber = /[0-9]/.test(password); + const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password); + + if (!hasLetter || !hasNumber || !hasSpecialChar) { + return false; + } + + return true; } module.exports = { validUsername, validPassword }; diff --git a/session-02/exercise.js b/session-02/exercise.js index 6ade818..61d0e3b 100644 --- a/session-02/exercise.js +++ b/session-02/exercise.js @@ -1,22 +1,25 @@ -/* - Transform the input array of strings into uppercase strings - For example, for the input ["cat", "hat"], return ["CAT", "HAT"] -*/ +/** + * Transform the input array of strings into uppercase strings. + * @param {string[]} listOfStrings - An array of strings to transform. + * @returns {string[]} An array of strings in uppercase. + */ function transformArrayToUpper(listOfStrings) { - return; + return listOfStrings.map(string => string.toUpperCase()); } -/* - Write a function that returns the sum of all student ages. - The function will be passed an array of objects and the result - will be the sum of all ages. - - Note, not all objects will contain an age. Omit these objects. - For example, for the input: - [{ name: 'Sandra', age: 31 }, {}, { name: 'Didi', age: 20}] - the function should return 51 -*/ +/** + * Write a function that returns the sum of all student ages. + * The function will be passed an array of objects, and the result + * will be the sum of all ages. + * Note, not all objects will contain an age. Omit these objects. + * @param {Object[]} listOfStudentObjects - Array of student objects. + * @returns {number} The sum of ages of students with an 'age' property. + */ function sumOfAllAges(listOfStudentObjects) { - return; + return listOfStudentObjects + .filter(student => student.age !== undefined) // Filter objects that have an age property + .reduce((sum, student) => sum + student.age, 0); // Sum up the ages } module.exports = { transformArrayToUpper, sumOfAllAges }; + diff --git a/session-03/js-patterns.js b/session-03/js-patterns.js index 5291252..21bdbe3 100644 --- a/session-03/js-patterns.js +++ b/session-03/js-patterns.js @@ -23,6 +23,9 @@ let driverAge = 16; // try changing to 16 or greater driverAge >= 16 && console.log("this person can drive"); // Ternary operator: ( ? : ) +let luckyNumber = 8; // Define luckyNumber +let guess = 8; // Define guess + let result = luckyNumber === guess ? "right" : "wrong"; // let result = "";