Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session 02 Completed #49

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions session-01/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/*
Expand All @@ -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 };
29 changes: 27 additions & 2 deletions session-02/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/*
Expand All @@ -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 };