SpaceComp
An introduction to intermediate JS
Today
Git Setup
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.
We’ll wait until next week for git commands.
Git Install for Windows
https://git-scm.com/downloads
Reviewing our JS
Table of Contents
- Maps
Intro to Maps
Why maps?
>>> var speed = new Map();
>>> speed.set(“map”, “fast”);
>>> speed.get("map");
'Fast'
>>> speed.get("map");
undefined
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.
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.
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.
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?
const keyword
e.g.
const num = 1;
num = 2; // Error!
const list = [];
list.push(“a”);
Exceptions
How to fail well.
Table of Contents
- Basic exceptions
Basic Exception
try {
console.log(“a”)
badFunc(); // not defined
console.log(“b”)
} catch {
console.log(“error!”); }�>>> a�>>> error!
Looks like “break” or “return”.
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.
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.
Exception Variable
try {
badFunc();
} catch (err) {
console.log(err);
}
We can get the data describing the exception by specifying a variable in the catch.
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.
Web Intro to Git
Guide recycled from previous project. Exact links and paths will not match.
Step 1 - Go to Repo
Step 2 - Click Fork
Step 3 - Go to the bots folder (files should be visible)
Step 4 - Click “Create new file” and paste your source code (or edit)
Step 5 - Save your changes; a commit appears
Step 6 - Click pull request then “Create pull request”
Step 7 - Optional comment and then submit
Bonus Material
(Co-op prep/ Advanced CS)
Table of Contents
- Algorithms
- Multithreaded code
- Databases
- Networking
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.
CS 341 - Algorithms in 3 minutes
Multiple strategies you can try
Divide and conquer
Greedy algorithms
Dynamic programming
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
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)
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
CS 348 - Databases
A database stores tables. (Like a folder)
A table is like a spreadsheet with a specific format.
Columns
Rows / Records
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)
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.
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.
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.
So you don’t forget
Next Time
What do you want to learn next?