Skip to content

Commit

Permalink
week 2 exercise finished
Browse files Browse the repository at this point in the history
  • Loading branch information
alanchen8647 committed Aug 10, 2024
2 parents 8b55aa3 + f710710 commit 956d136
Show file tree
Hide file tree
Showing 10 changed files with 476 additions and 1 deletion.
19 changes: 19 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!--
CTP STUDENTS
Use this pull request template to provide assignment submissions.
If you plan on continuing to work on the code, you can open the
pull request as a DRAFT. When done open the pull request.
-->

**Is the solution complete?**

[Yes/No]

**Did you run into any problems?**

[Describe...]

**Did you collaborate with others on this solution?**

<!-- Provide github usernames -->
22 changes: 22 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Tests

on:
push:
branches: ["main"]
pull_request:
types: [opened, synchronize, reopened, labeled]
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20.x
cache: "npm"
- run: npm ci
- run: npm run build --if-present
- run: npm test
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.
160 changes: 159 additions & 1 deletion session-01/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,167 @@ is a multi line
comment
*/

<<<<<<< HEAD
//console.log("Hello World")

let age = 15;
console.log("My age is",age)
agqw = "twantiy"
console.log(agqw.charAt(0))
console.log(agqw.charAt(0))
=======
/***** Printing output *****/
// for debug/info purposes
console.log("Hello CTP class");

// semicolons are optional, I recommend using them
// console.log("Hello CTP class")

/***** Variables *****/
// declaring variables
let age = 30;
console.log("value of age", age);
// mutable (it can change)
age += 5;
console.log("value of age", age);
age = "twenty";
console.log("value of age", age);
// constants (immutable)
const lastName = "Lopez";
console.log(lastName);
// 🚫 Can't update a constant variable
// lastName = "Molina";
// We can use unicode characters (careful when copying/pasting code)
age = "😢";
console.log(age);
// convention: use camelCase for our variable names and function names
let firstName = "Edgar";
let dateOfBirth = "1-1-1000";
// let first_name = "John"; // snake case

// avoid single letter vars, abbreviations, and 'temp'
let fn = "john";
let dob = "2-2";

// Older ES5 syntax (avoid)
// var declarations are hoisted to the top
luckyNumber = 12;
var luckyNumber = 7;
console.log(luckyNumber);

secondLuckyNumber = 7;
console.log(secondLuckyNumber);

/***** Data Types *****/
// Booleans
console.log(true);
console.log(false);

// Numbers (aka integers and floats)
console.log(11 + 31);
console.log(45.999);

// Strings
console.log("I <3 CTP");
console.log("Learning" + " - " + "JavaScript");

// null
console.log(null);

// undefined
let lonely;
console.log(lonely);

// mixing up data types (coercion)
console.log(3 + "4");
console.log(3 - "4");
console.log(3 + parseInt("4")); // parseInt converts to number

/**** Objects *****/
// Arrays
let array = ["array", "with", 4, "things", [2, 3]];
console.log(array);
console.log(array.length);
array.push(3.14);
console.log("length", array.length);
console.log("pushing", array.push(3.14));
console.log("printing", array);

// strings
let anotherName = "Sofia";
console.log(anotherName.length);
console.log(anotherName.toUpperCase());

// splitting strings
let stringData = "id123 Sandy Smith 42 NYC";
let dataParts = stringData.split(" ");
console.log(dataParts);

// objects, dictionary-like, map-like, key-value store
let obj = {
"first-name": "Sofia",
age: 31,
};

// set properties/keys
obj["school"] = "Baruch";
obj.school = "York";

console.log(obj);

// get the object properties
console.log(obj["first-name"], obj.age);

/***** Functions *****/
function add2(num) {
return num + 2;
}

let result = add2(5);
console.log(result);

/***** Conditionals *****/
// equality:
// 🚫 avoid loose equality (==) [applies coercion]
// ✅ use strict equality (===)
console.log(34 == 34);
console.log(34 == "34");

console.log(34 === 34);
console.log(34 === "34");
console.log("ed" === "ED");

// Not equals
console.log(34 !== 45);

// Logical operators: !, ||, &&
// Comparison operators: <, >, <=, >=, ===, !==

// Can do if-else-if and nesting as you expect
// if (conditional) {
// if (conditional) {
// } else {
// }
// } else if (conditional) {
// } else {
// }

/***** Looping *****/
// For loops
let schoolName = "Baruch";
for (let i = 0; i < schoolName.length; i++) {
console.log(schoolName[i]);
}

// while loop
let j = 0;
while (j < schoolName.length) {
console.log(schoolName[j]);
j++;
}
>>>>>>> origin/week-2
35 changes: 35 additions & 0 deletions session-01/notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,38 @@
7/31/2024

## What do we know about JavaScript?

- scripting language, runs line by line
- similar to Java, but simpler
- can update HTML using JS
- can use Node.js on the backend
- React/React Native runs on JavaScript
- most widely used languages in web development
- makes sites interactive

## ECMA Script

- ES5 (pre-modern JavaScript)
- ES6 / ES2015
- ES2020
- ES2024

## Backend work

Use Node.js. It allows JavaScript to function like C++ and Python. It has full access to hardware and computer resources.

Compiled vs Interpreted Programming languages

## Frontend work

In the browser, we're using JavaScript syntax, but can't access the local computer resources. It can only manipulate the HTML and CSS and use Web Browser API's.

## Searching Google for help

- use `mdn` as a keyword to search the mozilla docs
- _i.e._ Google search `mdn array add element`

## Resources for learning more about JavaScript

- https://eloquentjavascript.net/index.html
- https://exploringjs.com/js/book/index.html
32 changes: 32 additions & 0 deletions session-02/exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Transform the input array of strings into uppercase strings
For example, for the input ["cat", "hat"], return ["CAT", "HAT"]
*/
function transformArrayToUpper(listOfStrings) {
let UpperString = [];
for(const word of listOfStrings){
UpperString.push(word.toUpperCase());
}
return UpperString;
}

/*
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) {
let totalAge = 0;
for(const student of listOfStudentObjects){
if(student.age){
totalAge += student.age;
}
}
return totalAge;
}

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;
Loading

1 comment on commit 956d136

@alanchen8647
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Week 2 Exercise finished

Please sign in to comment.