Unit 1: Data & Objects
In this unit we will discuss various data types that assist with storage and organization of data. This will lead into Objects and an introduction to Object Oriented Programming.
1.0 - Primitive vs Abstract Data Types
Simple variables that can be stored in a byte of memory are called primitives. The typical primitive data types in a programming language are:
integer (byte, short, int, long, char)
float (float, double)
Note: characters (char) are actually integers
1.1 - Custom Data Types in JS
That allows us to keep track of the name of the switch and the status of it, all in one. Let's head over to Replit to see more examples and test it out ourselves.
1.2 - The Stack
Arrays provide access to any element at any time. But what if we only want to provide access to the last element placed in the array?
A stack is an abstract data type that we call Last In First Out (LIFO). Similar to a stack of pancakes or plates, the only one you have direct access to is the one on top - the last one placed on the stack.
A Stack employs the following functions:
Push, which adds an element to the collection,
Pop, which removes the most recently added element that was not yet removed, and
Peek, which returns the most recently added element without removing it from the stack.
Here is a visual representation of push and pop.
1.3 - Arrays are an Abstract Data Type!
We've been using Arrays like a magical bag of holding. Put whatever you want in there, remove things, etc... But have you given any thought as to how they work or what we would do without them?
1.4 - 2D Arrays
What if your pencil case could hold other pencil cases?
Think of a single array like a shelf holding jars.
Now think of a bookshelf with numbered shelves, each holding jars.
Need the third jar on the top shelf? myShelf
How about the second jar from the middle shelf? myShelf
Need the entire middle shelf? myShelf
When is this useful, you might ask? All the time! Think about games like Tetris, Pacman, Bejeweled, or Battleship.
Heads up! - Arrays in JS are passed & copied by reference
What does this mean - by reference?
It means that when you try to make a copy or pass an array into a function, it does not actually duplicate every item in the array to create separate arrays. It makes a link or shortcut to the array.
Practicing Data Manipulation & 2D Arrays
If you have not completed 1.4a and 1.4b, you should do so. If you are stumped on a particular function, ask a classmate or move on to this:
I am often asked "how do I become a better programmer?" and the simplest answer is - program! You need to practice and see patterns or repetitious problems / solutions in order to improve. It's like working construction - at first you have no idea what you're doing. After working on a few projects, you use your experiences to help inform your decisions.
Most of the problems in Code Chef involve data that is stored in a 2D array (that I named test_cases). I would like you to attempt the following practice problems on Code Chef and then branch out and try some other problems on your own.
The input loading code is below (same as in the Code Snippets project in Replit). Select NodeJS as your programming language in Code Chef and paste the input code, replacing anything already in there. You can save it as your template for future practice problems. Keep in mind that your output for any problem is through console.log(), not return, and it must match their description perfectly.
1.5 - Objects (Class Definitions & "New" Keyword)
Hang in there - this lesson is a real doozy.
Working with Properties
1.6 - Objects (The Constructor & Class Methods)
To be honest, the lessons that you see here are basically duplicated (and usually better) inside Replit. I'm not really sure why I keep posting the lesson in two different places. I suppose it's a "just in case" for students who can't get to Replit or if I fail to publish the project in Replit. This site is also public so parents and general community can see it. But I have to admit - I'm tired. So I'm going to go to bed now. ZZZzzz