Week 18 Homework đť đˇ
New stuff we learned this week: đ¤
Vim
- you can set a mark by typing
m
then a letter, likemm
to set a mark calledm
ormr
to set a mark namedr
- immediately jump to a mark youâve set by typing a backtick plus the letter of the mark, like `m
- marks with capital letters are global â they allow jumping to a mark across files, like
mM
ormR
- the
zz
command (from normal mode) centers the screen on the line your cursor is on
Javascript
- use LOTS OF WHITESPACE in your javascript programs to make it easier to understand
- you can make decisions in javascript using
if / else if / else
control flow statements:
let name = process.argv[2];
if (name === "jared") {
console.log("Old guys are cool!");
} else if (name === "willow") {
console.log("Goat banjo rodeo.");
} else {
console.log("Good morning,", name);
}
- you donât have to have a
else if
or andelse
at all, you can just have anif
:
if (age > 40) {
console.log("Wow you are old!");
}
- to compare if two things are equal in javascript, use 3 equal signs next to each other:
x === 5
- to test if something is less than or greater than use
x < 5
orx > 5
- to test if something is not equal use
!==
:x !== 5
- hereâs a summary of testing values:
let x = 5;
x === 5; // `true` - x is EQUAL TO `5`
x !== 7; // `true` - x is NOT EQUAL TO `7`
x < 6; // `true` - x is LESS THAN `6`
x > 4; // `true` - x is GREATER THAN `4`
x <= 5; // `true` - x is LESS THAN OR EQUAL TO `5`
x >= 2; // `true` - x is GREATER THAN OR EQUAL TO `2`
- you can also test booleans directly like so:
let student = {
name: "Bob",
likesGoats: true,
};
if (student.likesGoats) {
alert("Goat banjo rodeo!");
}
DOM Scripting & HTML
- just like in CSS you can put your javascript in an external file by putting a
src
attribute on an empty<script>
tag, like so:
<script src="./my-rad-code.js"></script>
- to add a new element with javascript you need to do 3 steps:
- create a new element
document.createElement(<tag-name>)
- make any modifications to the new, empty element
- âgive it a daddyâ â that is, attach it to a parent node
parent.appendChild(newElement)
- create a new element
// first, create a floating, unattached, empty element
let newParagraph = document.createElement("p");
// second, make any changes to the element you want
newParagraph.innerHTML = "goat banjo <b>rodeo!</b>";
// finally, attach new element to a parent in the DOM
let body = document.querySelector("body");
body.appendChild(newParagraph);
- you can use
Element.setAttribute()
to set attributes of DOM elements:
// modify an EXISTING dom element
let myDiv = document.querySelector("div");
myDiv.setAttribute("id", "added-by-js");
setAttribute
is particularly useful when creating new DOM nodes, like images:
// modify a NEW dom element
let newImg = document.createElement('img'):
newImg.setAttribute('src', './frog.jpg');
Useful Links:
Homework Plan:
- 1 day creating new flash cards
- 1 days reviewing all flash cards
- 1 day short
vim
practice (see below) - 1 days node practice
- 2 days touch-typing practice
- 2 days Web practice
- 1 day watch CCCS #16
Homework day 1
- create new flash cards
- review all flash cards
-
vim
practice
Homework day 2
- touch-typing practice
- node Homework #1
Homework day 3
- watch CCCS #16
- Web Homework #1
Homework day 4
- touch-typing practice
- Web Homework #2
Flashcard Assignment
- review your current stack of flashcards, but do it UPSIDE DOWN â that is, look at the backs and guess the fronts
- make 3 new
vim
cards:zz
mm
(or whatever makes sense to you how to write setting a mark)- `m (or whatever makes sense to you how to write jumping to a mark)
- make 5 new
javascript
cards:===
testing for equality!==
testing for inequalitydocument.createElement()
element.appendChild()
element.setAttribute()
Vim Homework
- separate all of your
vim
flashcards - review each of them, selecting 3 only that you donât use enough, but want to use more
ssh
in and spend 3 minutes practicing those 3 commands usingvimtutor
Node Homework
- slowly and carefully review the Javascript portion of âNew Stuffâ above ^.
- NOTE: for this homework, remember that arguments coming from
process.argv
are always strings. If you want to change a string to a number, use theNumber()
function,let myNum = Number(someVariable);
ssh
into your home dir, then move down into yournode
dir, and create a new directory calledweek18
- make a new file called
pick-a-number.js
that you can execute from the command line like this:
$ node pick-a-number.js 7
- the code should pull the number from
process.argv
and compare to a number you have pre-selected. If the number matches, thenconsole.log
out something likeYAY! You guessed my number!
. If it doesnât match, log out something likeNo bueno, try again
. So if your number was 3, interacting with the program from the CLI would look like this:
$ node pick-a-number.js 7
> No bueno, try again.
$ node pick-a-number.js 2
> No bueno, try again.
$ node pick-a-number.js 3
> YAY! You guessed my number!
next, make a new node script called
lower-higher.js
that works the same way as the previous script, except this time, if the guess is too low it logs outNo bueno, too low
and if the guess was too high it logs outNo bueno, too high
, and shows the same success message if they get the number right.next, make a new file called
htc-greet.js
so that it can be invoked from the shell like this:
$ node htc-greet.js jared
the script should look at the argument passed in (in the example above, it would be the string
"jared"
) and make a decision:- if the name is
jared
, console.log outGreetings, wise teacher!
- if the name matches any other of the students, it should log out a message which includes their name:
Greetings, fellow student Tabitha!
- if the name isnât
jared
or a studentâs name, log out âGreetings, stranger!â - note: make sure there isnât an extra space after the studentâs name and before the
!
- if the name is
next, you probably have 5 lines of code that are very similar, to log out the message for each of the 5 students in HTC. Fix this by extracting out a function called
greetStudent
â it should have one parameter calledname
and it should do the greeting part. When youâre done, youâll have 5 lines that call this function, passing in the name, likegreetStudent(name)
â and the script should work exactly the same as before.finally, use
chmod
to make your 3 node scripts executable by anybody, then post that youâre done on slack. By making them executable by anybody, I or any of the other students can ssh into your directory and run your scripts. đExtra Credit: ⨠copy your
htc-greet.js
file to a new file calleduser-greet.js
. It should work exactly the same, except instead of the user passing in a string as an argument, the script should determine the user from theprocess
itself. So, if done correctly anyone in HTC should be able to simply typenode user-greet.js
and it would correctly greet them. Be sure to make it executable by others as well. (note: my username is actuallyubuntu
notjared
)
Web Homework #1
- first, carefully and slowly review the DOM Scripting portion of âNew Stuffâ above ^.
- second, do the
vim
homework first â it only takes about 3 minutes ssh
into your home dir, then go down intowww
and make a new directoryweek18
, cd into that dir- make a new html file called
dogmeat.html
which works like this:- it should have an
<h1>
tag that saysWhat's for Dinner?
- below the
h1
should be an image of a kitten - below the kitten, there should be an empty
ul
element with no list items - after 1 second, a new list item should appear that reads
dogmeat
- after 2 seconds, a new list item should appear that reads
catmeat
- after 3 seconds, a new list item should appear that reads
dogmeat
- after 4 seconds, a new list item should appear that reads
catmeat
- it should have an
- next, add some cool CSS styling to the h1, image, and list (do this in a
<style>
tag in the<head>
) - save and close your webpage, and type
vimtutor
â then search forward toLesson 5.3
and do just that lesson - close
vimtutor
and return todogmeat.html
- using visual line mode and the
:w <new-file>
syntax you just practiced, extract your CSS and javascript both into external files - make 3 global vim marks:
H
(html),C
(css), andJ
(javascript) â for each of your three files - for the rest of the homework, practice using vim marks to jump around inside your file, and back and forth between the
.html
,.css
and.js
files with the global marks you made - edit your javascript file so that when one of the list items that says
dogmeat
is added, it gets a blue background and white text, and make thecatmeat
items have a red background with yellow text (or you can pick the colors, but at least two styles on each). - save and test in your browser
- next, change your code so that the styling for the dog vs cat items is in the CSS file, not in javascript, using two classes
.dog
and.cat
â and change your javascript file so that instead of doing the styling directly on the new element, it sets an attribute â giving the list item a class of eitherdog
orcat
. - Extra Credit â¨: change your webpage to use
setInterval
and a single function instead of multiple calls tosetTimeout
. After it is changed, the list should keep addingdogmeat
andcatmeat
list items forever, one per second, including the classes and styling. Also add a bit of text somewhere on the page that says âmake it stopâ and have it so that if you click that text, it stops adding new list items. - share you link in Slack!
Web Homework #2
ssh
into your home dir, thencd
down intowww/week18
- make three new files:
mouse.html
,mouse.css
, andmouse.js
- make global marks for each of your 3 files so you can jump around between them easily in vim
- create some basic HTML that:
- links to your external CSS stylesheet
mouse.css
- links to your external javascript file
mouse.js
- has a small (width 150px) image of a frog positioned in the upper right of the page
- has a small (width 150px) image of a goat positioned in the lower right of the page
- has an
h1
that saysFrog Mouse Events:
and under it an empty unordered list - below the empty unordered list, add another
h1
that saysGoat Click Events:
with itâs own empty unordered list below it.
- links to your external CSS stylesheet
- I didnât teach you this yet, but just like you can do
element.onclick = <some-function>
â there are other events you can hook to. Two fun ones areonmouseenter
andonmouseleave
, they work the same waysomeElement.onmouseenter = someFunction
- write some javascript so that whenever your mouse âentersâ the frog image, a list item is added to the first empty list which says âFrog image entered.â and likewise, when the mouse leaves the image, a list item should be created that says âFrog image leftâ. Or, you can make up your own silly text, as long as itâs different.
- save and test in the browser.
- next, make it so every time the goat image in the lower right is clicked, two things happen:
- first, the image should change to another animal
- second, the last empty unordered list you made should get a new list item that says
Switching to aardwolf
or whatever animal you are switching to
- make it so that if I click the image 4 times, it turns back into the goat image, and the cycle continues
- add some javascript so that after 30 seconds, the entire webpage is replaced by a message that says âthis web page has self-destructed, sorryâ.
- Extra Credit â¨: another cool event you can listen to is
onmousemove
. And, when a function that is an event listener (like something you set toelement.onclick
orelement.onmousemove
) is invoked, it actually receives a special object called the event as itâs first argument. Set up a listener on thebody
element that is invoked everytime the mouse moves. Figure out what the event object looks like somehow (hint: console.log is your friend). Once youâve examined the event argument, make it so that there is a paragraph at the bottom of the web page that is constantly displaying an up-to-date phrase showing the x and y coordinates of your mouse as you move it around the body. - Extra Credit â¨: make a 50px by 50px invisible div and absolutely position it somewhere on the page. Now, make it so that if the user mouses over it, it turns green and disables the self-distruct feature from step 10.
- Extra Credit â¨: change how you did the last step so that you donât use the
onmouseenter
oronmouseleave
events on the div â instead, use the mouse position from thebody.onmousemove
listener to calculate whether they are over the hidden div.