Week 14 Homework đť đ
New stuff we learned this week: đ¤
Vim
split <somefilename>splits your vim window horizontally loading the other file in the new section- use
<CTRL-w><CTRL-w>to switch between split screens - yanking and putting works across vim split screens
- you can also do
vsplit <somefilename>to split vertically
CSS
- a
display: inlineelement doesnât let you setwidthandheight display: inline-blockis likedisplay: inlineexcept it respectswidthandheightâ and it does NOT make a new line likedisplay: block- the
floatCSS property places an element on the left or right side of its container, allowing text and inline elements to wrap around it. The main values are:float: left,float: rightorfloat: none - The
clearCSS property sets whether an element must be moved below (cleared) floating elements that precede it:clear: leftorclear: rightorclear: both - âback in my dayâ web developers really only had floats and tables to try to lay out web pages â these were hacky and problematic, and led to the introduction of
flexboxandcss gridâ which we will cover later - CSS styles can be put into an external stylesheet and referenced by many web pages using a
linktag inside thehead:
<head>
<link href="main.css" rel="stylesheet" />
</head>Unix Wizardry
- an operating system is always running many tasks at once, these tasks are called processes. Each process has unique things like a
pid(process identifier), appid(parent process identifier), and an environment. - you can inspect the running processes by using the command
ps(process status)
Javascript
- javascript (js) arrays are like lists of things â they can contain items of any type, and can be any length, they are created by using square bracket notation:
let myArray = [1, 2, 3];- arrays can have any types within them, even other arrays:
let myArray = [1, true, null, "hello!", [3, 4]];- items in an array are accessed using square bracket notation, specifying the numeric index of the item you want, and the indexes start at
0
let myArray = [1, false, "bob"];
console.log(myArray[0]); // prints `1`
console.log(myArray[1]); // prints `false`
console.log(myArray[2]); // prints `"bob"`- javascript objects are like a list of labeled things â like drawers with labels. The labels are called âproperty namesâ or âpropertiesâ and the things in the drawer are the values. You create them with curly braces like so:
let myObject = {
name: "Jared",
age: 40,
hasBeard: true,
kidsAges: [13, 12, 9, 6],
};- items in a js object can be accessed using dot notation:
let myObject = {
day: "Tuesday",
temp: 31,
};
console.log(myObject.day); // prints `"Tuesday"`
console.log(myObject.temp); // prints `31`- functions in javascript are like little mini-programs. They are declared like this:
let mySweetFunction = function() {
console.log("I am a function. Beep boop. đ¤");
};- once a function is declared it wonât do anything unless you invoke (use) it. You invoke a function in javascript by adding parenthases after the function variable name:
mySweetFunction(); // logs out `I am a function ...`- functions can take zero or more arguments, which are variables or values passed into the the body of the function. This function takes two arguments,
aandb:
let adder = function(a, b) {
console.log("Sum is: ", a + b);
};
adder(2, 3); // logs out `Sum is: 5`Node.js
nodegives you some access to the computer process through theprocessvariable:
process.pid;
process.ppid;
process.title;
process.env;- the
processvariable is an object with a property calledstdout(standard out) which is a function you can use to write to stdout like so:
process.stdout.write("barf me to standard out please");- when you execute a node.js program and pass arguments from the command line, these are available to javascript as an array on the
processobject calledprocess.argv. So if execute a*.jsscript with extra arguments, like this:
$ node my-script.js foo bar// inside `my-script.js` I can access them like this:
console.log(process.argv); // logs `["node", "my-script.js", "foo", "bar"]
console.log(process.argv[2]); // logs `"foo"`
console.log(process.argv[3]); // logs `"bar"`Useful Links:
- Color Keywords
- Regexr â Regular expression playground
- TouchType.co
- How To Type
- CCCS #13
Homework Plan:
Note: we wonât be meeting Dec 21, so you can spread this out for 2 weeks
- 1 day creating new flash cards
- 2 days reviewing all flash cards
- 1 day
vimtutor - 2 day javascript practice
- 2 days touch-typing practice
- 2 days Web practice
- 2 day watch CCCS #13
Homework day 1
- create new flash cards
- review all flash cards
-
vimtutorâ everything
Homework day 2
- watch CCCS #13
- touch-typing practice
- Web homework #1
Homework day 3
- review all flash cards
- Javascript Practice #1
Homework day 4
- watch CCCS #13
- Web Homework #2
Homework day 5
- touch-typing practice
- Javascript Practice #2
Flashcard Assignment
- review your current stack of flashcards
- make one new
SHELLflashcard for the commandps - make 3 new
VIMflashcards for:split <filename>and:vsplit <filename>and<CTRL-w><CTRL-w> - make 3 new
CSSflashcards for:display: inline-blockfloat: leftorfloat: rightorfloat: noneclear: leftorclear: rightorclear: both
- make 2 new
NODEJScards for:process.argvprocess.stdout.write()
Web Homework #1
- make sure youâve done the complete
vimtutorfrom this weeks homework first - carefully read and review the
VimandCSSsections of âNew Stuffâ above ^ sshinto your home dir andcddown into yourwwwdir- use
cpand the correct flag to recursively copy the entireweek13directory, making a new directory calledweek14. (hint: not sure how? read up inman cp) cddown into your newweek14dir and usevimto open theanimals.htmlfile- move your cursor just below the opening
<style>tag and enter visual line mode by pressing<SHIFT-v>, then pressjuntil all of your CSS rule-sets are highlighted - now the command
:w styles.css(youâll see a'<,'>thing indicating youâre in visual line mode) â this will make a new file in your current directory calledstyles.csscontaining your highlighted text - now type
:split styles.cssand it should bring up a new section in vim showing you the new file you created - (if youâre lost or something went wrong in the last three steps, exit out of vim, and open up
vimtutorâ skip to section 5.2, and then practice sections 5.2 and 5.3 until you feel more comfortable) - with both of your sections open, practice using
<CTRL-w><CTRL-w>to bounce back and forth between sections - in the
animals.htmlfile, use visual line mode again (enter into that mode by<SHIFT-V>) to select the entire<style>tag and then delete it with thedoperator - now, inside the
<head>tag, add a<link>tag (for exact syntax see âNew Stuffâ above ^) linking to your external stylesheetstyles.css - save your
animals.htmland view it in a browser to make sure looks the same as it did before. If the page looks white and unstyled, something is probably wrong with your<link>tag - next, pick one of your animal chunks to delete - and delete it by first selecting the whole chunck with visual line mode, and then pressing
d - next, pick one more of your animals to delete, and this time delete the whole chunk using
datâ if your first attempt doesnât work, just use theukey to undo and try again, it matters where your cursor is when youâre doing things likedat - you should have two animals left now, for each one convert the lists into a single
<p>tag containing just all of the text from all 5 facts. See if you can use some of your vim skills to make this task a little faster. - next, move the
imgtag so that it is inside the firstptag that you just made, for both animals - over in your
styles.cssfile, re-style the images so that one is floated left, and the other right, and that they are small enough for the text to wrap around them nicely. You might want to increase the font-size of the paragraph text as well - now, tweak the size of the image and the font-sizes on the page until you get it to the point where the second paragraph tag from each chunk is partially flowing around the image, and partially below it
- finally, apply some css to the second paragraph to make sure it is entirely below the floated image, and not at all wrapping around it. Do this for both animals.
Web Homework #2
- NOTE: read through ALL the directions in this section before starting
- pick 3 of your favorite flashcards that youâve made so far for HTC
sshinto your home dir- in one command, from your home dir, make a new directory called
flashcardsinside yourwwwdir, with another directory inside of that calledv1 - in one command
cddown into thev1dir you created above - in this directory, youâre going to make a crude, mini website. Instead of telling you exactly what steps to do to build this, Iâm going to give you some basic requirements of what youâll need to have when youâre done, see below:
- when youâre done:
- you will have 6
<something>.htmlfiles, each having a<link>tag to a shared externalCSSstylesheet - 3 of the html pages will contain the fronts of your âflashcardsâ
- 3 of the html pages will contain the backs (or âanswersâ sort-of) of your âflashcardsâ
- the pages should have a non-white background, and there should be an HTML element styled to look sort of like a flashcard â white and rectangular
- each flashcard âfrontâ and âbackâ should have the âcategoryâ displayed somewhere (by category I mean like
REGEXorVIMorHTML, like on your real flashcards) - the pages should link to each other sequentially, so that you can post in Slack the URL to the first card âfrontâ, and I should be able to click to see the answer page, then click from there to see the next âfrontâ, and so on.
- there should be no
<style>tags or inline style attributes â all CSS must go in a single external stylesheet - any part of the flashcard fronts or backs that is code or code-like should be marked-up and styled so that it sort of looks like the red/grey inline code blocks on this homework page, like this one:
foo bar baz - the last card âbackâ should have a celebratory message and a picture of an animal, plus a link back to the beginning card âfrontâ
- you will have 6
- I would highly recommend starting with just one html file and your external stylesheet. Make a single card âfrontâ and get it looking the way you want it. Then you can just
cpthat file 5 times and make small adjustments to the copied file, changing the card contents and linking them together. - post the link to your first card âfrontâ in slack so we can see and test our knowledge against your flashcards!
JS Homework #1
- carefully review the
JavascriptandNodejssections of âNew Stuffâ above ^^ sshinto your home dir, make a new dir callednode, cd down into that, and make a new dir calledweek14, thencdinto that- make a new file with vim called
array.js - in that file, type three lines of javascript declaring 3 variables named
strings,numbersandbooleansâ each should be an array containing 3 or more values of the type indicated by the name, so for instance, forstringsyou could do:
let strings = ["goat", "banjo", "rodeo"];- on lines
4-6useconsole.log()to log out each of the three variables you just made - save, close, and execute the file by typing in the shell:
node array.jsâ you should see the three arrays you made logged out. - open the file again in vim and this time, make a new variable called
arrayOfArraysâ and make it an array containing the three arrays you alread made. You donât have to re-type the whole arrays, just use the variable names in the new array. - add a
console.log()statement to log out your newarrayOfArraysmega-array. - save, close, and execute the file to see your big new array logged out in the shell
- edit the file again, and delete all of the
console.logstatements, leaving just the variable declarations, there should be 4 lines of code when youâre done deleting. - next, add three new lines of
console.log()statements, but in these, use square bracket notation to log out the first item ofstrings, the second item ofnumbers, and the third item ofbooleans. - Extra credit: ⨠add three more lines of
console.logaccessing the same values, but access them within thearrayOfArraysvariable - save, close, and execute in the shell. if you get errors, fix them until it logs out what you expect
- next make a new file called
object.js - in
object.jsdeclare a variable calledmyObjectthat is an object containing 6 properties - you can name them whatever you want, but each one should contain a value of a different type, including:number,string,boolean,null,undefined, andarray. - add a line to
console.log()out yourmyObjectvariable - save, close, and execute the script from the shell
- edit the
object.jsfile again and remove the console.log, but add 5 more lines logging out each property one at a time using dot notation (vimâsyypis your friend here to make it faster) - save, close, and execute the file in the shell, it should log out what you expected
- edit the
object.jsfile again, and position your cursor on the first character of the firstconsole.logline. Use the vim commandct(to changeconsole.logtoprocess.stdout.write - next, move down a line and use the
.command to repeat the last change â note it will only work correctly if your cursor is on the beginningccharacter - make sure you understand why
ct(worked, you should be able to express in english what it means â if you donât know why it worked, ask me in Slack. - save, close, and execute the file from the shell â you should get an error message. Try reading the error message to see what you think it means before continuing.
- edit the file again and fix the error â the error is caused because
process.stdout.writeonly takes strings, so to fix it, wrap the values youâre writing in a special function calledString()that converts values to strings, so for instance like this:
process.stdout.write(String(myObject.foo));- save, close and execute the file â you should have no errors, but youâll notice that all of the values got globbed together on one line â
process.stdout.writedoes not add newlines likeconsole.log()does - edit the
object.jsfile again, and add 5 new lines to put newlines between each of yourprocess.stdout.write()calls â pass the string"\n"in â thatâs the ânewlineâ character in js (and most programming languages) - save, close, and execute the file â it should look like it did back when we were using
console.log() - make a new file called
process.js- in it,console.log()out 3 things:- the process id
- the process parent id
- the process title
- Extra credit: ⨠figure out one or two of the property names of the processâs environment
process.envand log those out too`
- save, close and execute â you should see values that make sense and no errors
- edit the
process.jsfile again, and this time add an additional log statement, where you log outprocess.argv - save, close, and execute the file â do you understand what the
process.argvis? - try executing the file with random arguments, like
node process.js foo bar 1 3 hello - based on what you learned above, and what you know about array square bracket notation for accessing array items, change
process.jsso that when you execute it by typingnode process.js WOWyou log out just the stringWOW
Javascript Homework #2
sshinto your home dir, andcdinto thenode/week14dir- with vim, create and edit a new file called
fns.js - in that file create a new function that takes no arguments, but logs out the string:
My name is <yourname>.but use your own name. - below the function declaration, invoke the function 3 times in a row
- save, close, and execute the file â it should look like this:
$ node fns.js
My name is Tabitha.
My name is Tabitha.
My name is Tabitha.- edit the file again, and change the function so that it takes one argument called
name, then change the three invocations so that you pass in three names:"Shadrach","Meshach", and"Abednego" - save, close, and execute the file â it should look like this:
$ node fns.js
My name is Shadrach.
My name is Meshach.
My name is Abednego.- make a new file called
greeting.js, in it make a function calledgreetthat takes no arguments, but rather reads a single argument fromprocess.argvand displays a greeting message that says,Hi there <somename>, glad to see you!. If youâve done it write you should be able to invoke it from the shell with different names and get different outputs, like this:
$ node greeting.js Bob
Hi there Bob, glad to see you!
$ node greeting.js Leo
Hi there Leo, glad to see you!- finally, make a new file called
multiplier.jsâ write javascript in it such that you can execute it from the shell with 3 arguments - one string and two numbers, and it should work like this (see hints below for help):
$ node multipler.js Betty 3 4
Hola Betty! 3 times 4 is 12.
$ node multipler.js Sally 5 10
Hola Sally! 5 times 10 is 50.- hints for last problem:
- the
*operator does multiplication in javascript, notx - if you need to turn a string into a number, use the
Number()function:Number(someString) - youâll need to use
process.argvand square bracket notation to pull the right arguments from the user
- the
- Extra Credit:â¨: edit the code in
multiplier.jsso that you have a function that only does the muliplication, then use that function when displaying the result (hint: youâll need to use thereturnkeyword)