diff --git a/session-01/validators.js b/session-01/validators.js index 56ea66d..49578a3 100644 --- a/session-01/validators.js +++ b/session-01/validators.js @@ -7,7 +7,35 @@ - username cannot contain special characters */ function validUsername(username) { - return; + + let goodUsername = false; + + // username must be between (and including) 3-10 characters in length + if(username.length >= 3 && username.length <= 10) { + + // username must begin with a letter + /* + /^[a-zA-Z]/ is the regex that test if a expression's first character is a letter + */ + if(/^[a-zA-Z]/.test(username)) { + + // username may contain numbers and letters + /* + /^[A-Za-z0-9]*$/ is the regex that test if a expression contains both number and letter + */ + if (/^[A-Za-z0-9]*$/.test(username)) { + + // username cannot contain special characters + /* + /[^a-zA-Z0-9]/ is the regex that test if a expression contains special character + */ + if (!(/[^a-zA-Z0-9]/.test(username))) { + goodUsername = true; + } + } + } + } + return goodUsername; } /* @@ -17,7 +45,23 @@ function validUsername(username) { - password must contain at least 1 letter, 1 number, and 1 special character */ function validPassword(password) { - return; + let goodPassword = false; + + // password must contain at least 1 letter, 1 number, and 1 special character + /* + - [A-Za-z\d] Ensures that the first character is an alphabet or digit. + - [A-Za-z\d!@#$%^&*()_+]{7,19} will match minimum 7 maximum 19 character. This is required as he presceding character class + would consume a single character making the total number of characters in the string as minimum 8 and maximum 20. + - $ Anchors the regex at the end of the string. Ensures that there is nothing following our valid password + - / at the beginning and end of regex tells the beginning and end of your regex expression + */ + + if (/^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d][A-Za-z\d!@#$%^&*()_+]{9,63}$/.test(password)) { + goodPassword = true; + } + + return goodPassword; } +// Test to see that git is working and is able to push to repo on github. module.exports = { validUsername, validPassword }; diff --git a/session-02/exercise.js b/session-02/exercise.js index 6ade818..6131f3b 100644 --- a/session-02/exercise.js +++ b/session-02/exercise.js @@ -3,7 +3,15 @@ For example, for the input ["cat", "hat"], return ["CAT", "HAT"] */ function transformArrayToUpper(listOfStrings) { - return; + + /* + - map() method: 使用 map() method 来创建一个 array by applying a function to each element of the array. + - word.toUpperCase(): 使用 .toUpperCase() 来转换到大写 + - Arrow function: The arrow function word => word.toUpperCase() takes each string in the strings array and returns its .toUpperCase version. + + */ + const upperCase = listOfStrings.map(word => word.toUpperCase()); + return upperCase; } /* @@ -16,7 +24,24 @@ function transformArrayToUpper(listOfStrings) { the function should return 51 */ function sumOfAllAges(listOfStudentObjects) { - return; + // initializing ageTotal to keep track and add all ages + let ageTotal = 0; + + //用 .forEach 来循环所有 element of object Array 里面的 age + listOfStudentObjects.forEach(ageFromElement => { + + //用 if condition 来 omit 掉所有不符合的年龄 like empty age or 小于 0岁 + if (ageFromElement.age >= 0) { + + // 所有符合要求的年龄加到 ageTotal + ageTotal += ageFromElement.age; + } + // else { + // console.log("Age is invalid"); + // } + }); + + return ageTotal; } module.exports = { transformArrayToUpper, sumOfAllAges };