From 1f8b5b8dabf9499c390c040c77aa8228ebe9a91e Mon Sep 17 00:00:00 2001 From: minglegao2001 <71712980+minglegao2001@users.noreply.github.com> Date: Tue, 6 Aug 2024 20:57:04 -0400 Subject: [PATCH 1/3] Finished validators.js Assignment --- session-01/validators.js | 47 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/session-01/validators.js b/session-01/validators.js index 56ea66d..811e253 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,22 @@ 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; } module.exports = { validUsername, validPassword }; From 1f71e8db3f55bc939062722a2ed7e2e58cac7ac2 Mon Sep 17 00:00:00 2001 From: minglegao2001 Date: Tue, 6 Aug 2024 21:11:41 -0400 Subject: [PATCH 2/3] testing to see commit is working for validators.js --- session-01/validators.js | 1 + 1 file changed, 1 insertion(+) diff --git a/session-01/validators.js b/session-01/validators.js index 811e253..49578a3 100644 --- a/session-01/validators.js +++ b/session-01/validators.js @@ -63,4 +63,5 @@ function validPassword(password) { return goodPassword; } +// Test to see that git is working and is able to push to repo on github. module.exports = { validUsername, validPassword }; From 7c2c84efaf7888574f84370a44a26ca8cc1c3c68 Mon Sep 17 00:00:00 2001 From: minglegao2001 Date: Tue, 13 Aug 2024 21:09:13 -0400 Subject: [PATCH 3/3] finished session-02 assignment --- session-02/exercise.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) 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 };