-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'CUNYTechPrep:main' into main
- Loading branch information
Showing
6 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |