Skip to content

Commit

Permalink
Merge branch 'CUNYTechPrep:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Randit-07 authored Aug 14, 2024
2 parents 1132ad7 + f710710 commit 093bf54
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
22 changes: 22 additions & 0 deletions session-02/exercise.js
Original file line number Diff line number Diff line change
@@ -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 };
47 changes: 47 additions & 0 deletions session-02/exercise.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
34 changes: 34 additions & 0 deletions session-02/functions.js
Original file line number Diff line number Diff line change
@@ -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;
55 changes: 55 additions & 0 deletions session-02/loops.js
Original file line number Diff line number Diff line change
@@ -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);
68 changes: 68 additions & 0 deletions session-02/objects-arrays.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 093bf54

Please sign in to comment.