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-01 assignment completed #22

Closed
wants to merge 5 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
33 changes: 31 additions & 2 deletions session-01/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

3 changes: 3 additions & 0 deletions session-03/js-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down