diff --git a/README.md b/README.md index bce8217..e6c1cef 100644 --- a/README.md +++ b/README.md @@ -33,3 +33,8 @@ After each session you can: - complete [validator functions](session-01/validators.js) - Due: August 7, 2024 by 12pm. + +### Session 02 + +- complete [exercise functions](session-02/exercise.js) +- Due: August 14, 2024 by 12pm. diff --git a/session-02/exercise.js b/session-02/exercise.js new file mode 100644 index 0000000..6ade818 --- /dev/null +++ b/session-02/exercise.js @@ -0,0 +1,22 @@ +/* + Transform the input array of strings into uppercase strings + For example, for the input ["cat", "hat"], return ["CAT", "HAT"] +*/ +function transformArrayToUpper(listOfStrings) { + return; +} + +/* + 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 +*/ +function sumOfAllAges(listOfStudentObjects) { + return; +} + +module.exports = { transformArrayToUpper, sumOfAllAges }; diff --git a/session-02/exercise.test.js b/session-02/exercise.test.js new file mode 100644 index 0000000..9d1e393 --- /dev/null +++ b/session-02/exercise.test.js @@ -0,0 +1,47 @@ +const { transformArrayToUpper, sumOfAllAges } = require("./exercise.js"); + +// describe(stringDescriptionOfTheGroupOfTests, functionThatHoldsTests) +// test(stringDescription, functionThatDoesTheTest) + +describe("transformArrayToUpper()", () => { + test('should convert ["abc"] to ["ABC"]', () => { + expect(transformArrayToUpper(["abc"])).toEqual(["ABC"]); + }); + + test('should convert ["apple", "banana", "coconut"] to ["APPLE", "BANANA", "COCONUT",]', () => { + expect(transformArrayToUpper(["apple", "banana", "coconut"])).toEqual([ + "APPLE", + "BANANA", + "COCONUT", + ]); + }); + + test("should convert [] to []", () => { + expect(transformArrayToUpper([])).toEqual([]); + }); +}); + +describe("sumOfAllAges()", () => { + test("should return 0 for empty input", () => { + expect(sumOfAllAges([])).toBe(0); + }); + + test("should return 0 for inputs missing ages", () => { + expect(sumOfAllAges([{ gpa: 4.0 }, {}, { name: "Sally" }])).toBe(0); + }); + + test("should return 51 for inputs with ages summing to 51", () => { + expect( + sumOfAllAges([ + { name: "Sandra", age: 31 }, + { name: "Didi", age: 20 }, + ]) + ).toBe(51); + }); + + test("should return 51 and ignore inputs with missing ages", () => { + expect( + sumOfAllAges([{ name: "Sandra", age: 31 }, {}, { name: "Didi", age: 20 }]) + ).toBe(51); + }); +}); diff --git a/session-02/functions.js b/session-02/functions.js new file mode 100644 index 0000000..6229983 --- /dev/null +++ b/session-02/functions.js @@ -0,0 +1,34 @@ +// Named functions +function average(a, b, c) { + return (a + b + c) / 3; +} + +// let result = average(1, 5, 23); +// console.log(result); + +console.log("calling average function: ", average(15, 20, 23)); + +// anonymous / unnamed functions +const add2 = function (a, b) { + return a + b; +}; + +console.log("call add2: ", add2(4, 6)); +console.log("print add2: ", add2); + +// Arrow functions (ES6) +const add3 = (a, b) => { + // a lot of logic + return a + b; +}; + +// implicit return with single expression functions +// (no curly braces {}, and no return statement) +const add4 = (a, b) => a + b; // lambda-like + +console.log("call add4: ", add4(3, 4)); +console.log("print add4: ", add4); + +// single parameter arrow function +// parentheses are optional +// const increaseBy3 = num => num + 3; diff --git a/session-02/loops.js b/session-02/loops.js new file mode 100644 index 0000000..adb7787 --- /dev/null +++ b/session-02/loops.js @@ -0,0 +1,55 @@ +// looping through arrays + +const fruits = ["apple", "banana", "coconut", "durian"]; + +// While loop +// let i = 0; +// while (i < fruits.length) { +// console.log(fruits[i]); +// i++; +// } + +// for loop +// for (let i = 0; i < fruits.length; i++) { +// const fruit = fruits[i]; +// console.log(fruit); +// } + +// for-of loop (trade off, don't get the index) +for (const fruit of fruits) { + console.log(fruit); +} + +// 🚫 avoid for-in loops +// for (const item in fruits) { +// console.log(item); +// } + +// .forEach +// function myPrint(item) { +// console.log("Delicious: ", item); +// } +// fruits.forEach(myPrint); + +let result = fruits.forEach((val) => { + console.log("Yucky:", val); +}); + +console.log(result); + +// callback function + +// .map() // transform/convert/change + +let pluralFruits = fruits.map((f) => f + "s"); + +console.log(pluralFruits); +console.log(fruits); + +// .filter +const words = ["spray", "elite", "exuberant", "destruction", "present"]; + +const filteredResult = words.filter((word) => word.length > 7); + +console.log(filteredResult); +console.log(words); diff --git a/session-02/objects-arrays.js b/session-02/objects-arrays.js new file mode 100644 index 0000000..2e34919 --- /dev/null +++ b/session-02/objects-arrays.js @@ -0,0 +1,68 @@ +const student243 = { + firstName: "Edwin", + lastName: "Cruz", + age: 29, + gpa: 4.0, + fullName: function () { + return this.firstName + " " + this.lastName; + }, + bday() { + console.log("Happy Birthday!"); + this.age++; + }, +}; + +console.log(student243); +student243.bday(); +console.log(student243); +console.log(student243.fullName()); + +// reassignment (not allowed with constant variables) +// student243 = { firstName: "Sally", age: 42 }; + +// referencing properties +student243.age; +student243["firstName"]; + +// adding properties +student243.major = "Science"; +student243["minor"] = "Math"; + +console.log(student243); + +// removing properties +delete student243.major; +console.log(student243); + +// does the property exist +if (student243.major) { + console.log("Major:", student243.major); +} else { + console.log("Major not found"); +} + +// get all property names +let allPropertyNames = Object.keys(student243); +console.log(allPropertyNames); + +// Arrays +const fruits = ["apple", "pear", "cherry", "plum"]; +const things = ["a", 42, null, { name: "Juan" }]; + +// length +console.log(fruits.length, things.length); + +// indexing +console.log(fruits[1]); +console.log(fruits[12]); + +// properties +console.log(Object.keys(things)); // don't do this + +// adding / push +fruits.push("Durian"); +console.log(fruits); + +// pop / shift +console.log(fruits.pop(), fruits.shift()); +console.log(fruits.length);