1 of 40

SpaceComp

An introduction to intermediate JS

2 of 40

Today

  • Review some key concepts
  • Learn Exceptions
  • Web intro to Git
  • f-strings and spread operator
  • Explore some concepts possibly useful for interviews
    • Dynamic programming
    • Threads
    • Networking

3 of 40

Git Setup

4 of 40

Download Git

Mac and Linux users should have a tool called “git” in their command line.

Windows users need https://git-scm.com/downloads to get Git Bash shell.

  • Installation is about as bad as Python- so be prepared

We’ll wait until next week for git commands.

5 of 40

Git Install for Windows

https://git-scm.com/downloads

6 of 40

Reviewing our JS

Table of Contents

- Maps

  • Sets
  • Static methods

7 of 40

Intro to Maps

Why maps?

  • Easily iterable
    • E.g. For loops
  • Fast

>>> var speed = new Map();

>>> speed.set(“map”, “fast”);

>>> speed.get("map");

'Fast'

>>> speed.get("map");

undefined

8 of 40

Sets

>>> mush = new Set(rocket);

>>> mush

{'o', 'n', 'a', 'c', 'r'}

>>> mush.delete("o");

>>> mush.add("hmm");

>>> mush

Set(5) { 'r', 'a', 'c', 'n', 'hmm' }

Remember arrays / lists are ordered

Sets are not and do not have duplicates.

9 of 40

Static Methods

class Student {

action() { this.describe(args) }

static describe(args) {

console.log(“The arg is “ + args);

}

}

Methods are like functions for classes. Static methods are more optimized than regular methods.

10 of 40

Frequent Questions

Let vs var

let is a more restricted version of var.

var declares a variable for the global scope or function scope.

let declares a variable within the current block.

if (condition) {

var a = "b";

let c = "d";

}

The variable a is visible outside the if block but the variable c isn't.

11 of 40

Frequent Questions

forEach?

Imagine we have a map called main from [ID -> Map]

main.forEach((item, id) => {

[ item is now every value ]

[ id is now every key ]

[ And the { ... } is like a mini function where (item, id) are passed like arguments ]

}

What if we use var inside { … }? How about let?

12 of 40

const keyword

  • Acts like let
  • The reference cannot be changed
  • But the value may be changed!

e.g.

const num = 1;

num = 2; // Error!

const list = [];

list.push(“a”);

13 of 40

Exceptions

How to fail well.

Table of Contents

- Basic exceptions

  • Else and finally
  • Raising exceptions
  • Custom exceptions

14 of 40

Basic Exception

try {

console.log(“a”)

badFunc(); // not defined

console.log(“b”)

} catch {

console.log(“error!”); }�>>> a�>>> error!

Looks like “break” or “return”.

15 of 40

Finally

try {

badFunc();

} catch {

console.log(“error!”);

} finally {

console.log(“finally!”);

}

>>> error!�>>> finally!

Finally:

Always runs no matter what happens (success or failure). Unless the computer is literally powered off.

16 of 40

Finally Example

function test() {

try {

badFunc();

} catch {

return 1;

} finally {

console.error(‘fin’);

}}

test();�>>> fin�>>> 1

Despite the return statement we still execute the finally.

Finally always runs at the end.

But the 1 appeared last since it was a return value.

17 of 40

Exception Variable

try {

badFunc();

} catch (err) {

console.log(err);

}

We can get the data describing the exception by specifying a variable in the catch.

18 of 40

Throwing Exceptions

try {

throw “some string”;

} catch (err) {

console.log(err);

}

>>> some string

We can throw string, numbers, booleans, and objects.

Try-catch and throw act like limited goto.

19 of 40

Web Intro to Git

Guide recycled from previous project. Exact links and paths will not match.

20 of 40

Step 1 - Go to Repo

21 of 40

Step 2 - Click Fork

22 of 40

Step 3 - Go to the bots folder (files should be visible)

23 of 40

Step 4 - Click “Create new file” and paste your source code (or edit)

24 of 40

Step 5 - Save your changes; a commit appears

25 of 40

Step 6 - Click pull request then “Create pull request”

26 of 40

Step 7 - Optional comment and then submit

27 of 40

Bonus Material

(Co-op prep/ Advanced CS)

Table of Contents

- Algorithms

- Multithreaded code

- Databases

- Networking

28 of 40

Algorithmic Questions

Know your data types.

Helpful

Start with a solution which works. Then try to optimize.

Decide whether to sort a list.

Binary search is O(log n) time.

e.g. You have two lists A and B. Find all unique elements in A not in B.

29 of 40

CS 341 - Algorithms in 3 minutes

Multiple strategies you can try

Divide and conquer

  • Split problem into smaller pieces (think binary search)

Greedy algorithms

  • E.g. Coin changing, pick the largest denomination first

Dynamic programming

  • Recursion but bottom-up rather than top-down
  • e.g. Fibonacci sequence with for loop

30 of 40

CS 350 - Operating Systems (Two Programs)

Program 1�print(“a1”)

print(“b1”)

print(“c1”)

Program 2�print(“a2”)

print(“b2”)

print(“c2”)

Programs run top-down one line at a time.

When running multiple programs the OS will pause and resume at arbitrary points.

E.g.

a1, b1, a2, c1, b2, c2

31 of 40

CS 350 - Operating Systems (Two Threads)

Thread 1�A = 0

A = 100

Thread 2

B = A + 10

A = B

A program (process) can run more than one thread at once.

Threads will often share variables. This can cause new kinds of bugs.��Why?

So we don’t freeze when waiting for

a task.

E.g.

Which possible final A values can we find?

A = 10, 100, 110, crash (if A not defined)

32 of 40

CS 350 - Operating Systems (Locks)

Thread 1�A = 0�lock.acquire()

A = 100

lock.release()

Thread 2

lock.acquire()

B = A + 10

A = B

lock.release()

A lock is a special type of variable.�It can only be acquired at most once. Otherwise the thread must wait.

E.g.

How many possible final A values can we find?

A = 10, 100, 110, crash

33 of 40

CS 348 - Databases

A database stores tables. (Like a folder)

A table is like a spreadsheet with a specific format.

Columns

Rows / Records

34 of 40

CS 348 - Why Databases?

Quickly search by criteria.

E.g. select name from students where GPA > 50;

We retrieve every value for name where the row’s GPA is greater than 50 in the students table.

Redundancy / Replication (spread data across multiple computers)

Speed (there are many many optimizations at work, b-trees, indexing, partitions, etc)

35 of 40

CS 456 - Networking

We need to send data between two programs.�Protocols exist at different levels of abstraction.

e.g. HTTPS for websites is built on top of a layer of TCP.

There are two common “low-level” protocols, TCP and UDP.

36 of 40

TCP vs UDP

TCP

Will guarantee the message arrives.

Will guarantee ordering. Will not receive a packet until all previous ones were found.

Will guarantee integrity. No corrupted data.

UDP

Only guarantees integrity. Of those that arrive.

Much faster.

37 of 40

Picking TCP vs UDP

TCP

Useful for chat messages or loading a website.

UDP

Useful for calls. If a millisecond of audio is corrupted, we don’t need to replay it.

38 of 40

So you don’t forget

39 of 40

Next Time

  • Variable scope
  • F-strings
  • Spread operator
  • “instanceof” keyword
  • Git by command line

What do you want to learn next?

40 of 40