Class: wdi-cc-3: salty-sardines
Duration: 3 hrs approx.
Creator: Thom Page
Modified by: Kristyn Bryan and Reuben Ayres
Topics: Terminal, boolean expressions, reading code, while loops, for loops
A long time ago in a Unix environment far, far away, young Jedi padawans who knew only of desktop software were seduced by the dark side of the Force to enter… The Terminal.
Follow the instructions below using all the console commands introduced in Fundamentals, class, or that you find on your own.
-
Open the Terminal app
-
Make a directory in today's folder called
homework
. Create a new directory in thathomework
directory calledhomework_part_1
and enter it. -
Create a file called
commands.txt
. -
Paste the answer to each numbered question (i.e. the command(s) that accomplished the task) in
commands.txt
once you get it to work.
-
Create a directory called
death_star
, and make the following files inside of it:darth_vader.txt
,princess_leia.txt
,storm_trooper.txt
-
In
galaxy_far_far_away
, make a directory namedtatooine
and create the following files in it:luke.txt
,ben_kenobi.txt
. -
Inside of
tatooine
make a directory calledmillenium_falcon
, and in it create:han_solo.txt
,chewbaca.txt
- You can rename a file using the
mv
command.
- Rename
ben_kenobi.txt
toobi_wan.txt
.
- You can copy a file from one location to another using the
cp
command. (man cp
for more info)
- Directories can be sibling (parrell to each other) or can be parents (the folder that contains the folder you are in)
- Copy
storm_trooper.txt
fromdeath_star
totatooine
.
- You can use the
mv
command to move files from one location to another.mv
can be used for renaming, moving, or both. Runman mv
to see the options—remember hit theQ
key to get out of the manual page viewer.
-
Move
luke.txt
andobi_wan.txt
to themillenium_falcon
. -
Move
millenium_falcon
out oftatooine
and intogalaxy_far_far_away
. -
Move
millenium_falcon
intodeath_star
. -
Move
princess_leia.txt
into themillenium_falcon
.
BE CAREFUL WITH rm
!!! THERE IS NO "TRASH" IN THE UNIX CLI. WHEN YOU DELETE SOMETHING IT IS GONE FOREVER!!!
You can use rm
to delete a file.
- Delete
obi_wan.txt
.
-
In
galaxy_far_far_away
, make a directory calledyavin_4
. -
Move the
millenium_falcon
out of thedeath_star
and intoyavin_4
. -
Make a directory in
yavin_4
calledx_wing
. -
Move
princess_leia.txt
toyavin_4
andluke.txt
tox_wing
. -
Move the
millenium_falcon
andx_wing
out ofyavin_4
and intogalaxy_far_far_away
. -
In
death_star
, create directories fortie_fighter_1
,tie_fighter_2
andtie_fighter_3
. -
Move
darth_vader.txt
intotie_fighter_1
. -
Make a copy of
storm_trooper.txt
in bothtie_fighter_2
andtie_fighter_3
. -
Move all of the
tie_fighters
out of thedeath_star
and intogalaxy_far_far_away
.
BE CAREFUL WITH rm
!!! THERE IS NO TRASH CAN IN THE UNIX CLI. WHEN YOU DELETE SOMETHING IT IS GONE FOREVER
Before you hit enter, make sure are deleting the right thing, or you could accidentally delete the contents of your computer (it has happened).
This command will not typically ask you if you "really want to delete." It will just delete.
- Remove
tie_fighter_2
andtie_fighter_3
.
-
Touch a file in
x_wing
calledthe_force.txt
. -
Destroy the
death_star
and anyone inside of it. -
Return
x_wing
and themillenium_falcon
toyavin_4
.
"Add" your changes (prepare them to be "committed"):
$ git add -A
"Commit" your changes—any time you make a commit, you can always restore the files in the repo to that point:
$ git commit -m "Completed homework assignment"
"Push" your commits to github:
$ git push origin master
-
Go back up to the
homework
directory and make another directory calledhomework_part_2
. So inhomework
you should havehomework_part_1
andhomework_part_2
. Do all these JavaScript questions inhomework_part_2
. -
Build our folder structure like we did in class.
-
Write your answers inside
app.js
. -
For any answers that require a written response, write the response as comments. For example:
// 1. The difference between interpolation and // concatentation is . . .
-
For coded responses, as you work on them, you may want to comment out everything else but the problem you're working on so that when you load
index.html
in your browser, your console is less cluttered. -
Before submitting, uncomment your answers to all coded responses so that the output for each question automatically runs in the terminal. We should not have to edit your code to make the file run.
-
A
Good luck!
-
What is the difference between interpolation and concatenation? Give an example of each.
-
What does the acronym DRY stand for? Why should we pay attention to it? What programming tools have we learned to write DRY code?
-
What is the difference between declaring a variable and assigning a value to a variable? What do we mean when we say "define" a variable?
-
When should we use
const
and when should we uselet
? -
Unix question: What is a "parent" directory?
-
Unix, again: If you're not sure about how to use a Unix command, how can you get more info without being connected to the internet?
-
More Unix: What is "tab completion" and why is it aweseome?
Remember that error messages are your friend. Even experienced developers constantly make syntax errors, they just have gotten faster at fixing them. They way they got faster at it is by reading error messages over and over and over and learning to understand them.
... and variable assignment
- Remember: Expressions in JavaScript are anything that can be said to have a value.
- Remember: Variable assignment is a right-to-left operation: the expression on the right side of the equal sign gets evaluated AND THEN put into the variable declared on the left side.
- Using the provided variable definitions, replace the blanks with mathematical or boolean operators that make the expression evaluate to
true
. - Test your answers by using
console.log
in front of each expression in your answer file.
const a = 4;
const b = 53;
const c = 57;
const d = 16;
const e = 'Kevin';
- a __ b;
- c __ d;
- 'Name' __ 'Name';
- a __ b __ c;
- a __ a __ d;
- e __ 'Kevin';
- 48 __ '48';
Increase your code literacy by reading code, line by line. When you read code pretend to be the computer. It is absolutely essential that you understand what is happening each line of code you write.
Read the following code very carefully.
DO NOT RUN the code because it might run an infinite loop.
Answer the following question:
- is this an infinite loop? Why or why not?
You cannot test this code, so read it line by line and 'execute' the code in your head.
while (true) {
console.log('Do not run this loop');
}
Infinite or not infinite? Give it a good guess. It is 100% OK if you get it wrong (as long as you don't run the code). It's important to get into the habit of reading code.
- is the following loop an infinite loop? Why or why not?
const runProgram = true;
while (runProgram) {
console.log('Do not run this loop');
runProgram = false;
}
Inifnite or not infinite? What is the expected output?
Ok rest easy, no more infinite loops for now!
The following while loop uses a compound assignment operator +=
. There are other compound assignment operators for other mathematical operations.
If you need a refresher on what the compound assignment operator does, have a look back at the afternoon lesson.
Without running this loop, what is the expected output?
Read the code line by line—everything happens in sequence. Add a comment before each line of code explaining what the line below it does. Then write down what output you expect to see in the console. Be patient with your thought! There is no rush. Time spent to really understand these fundamentals now will pay off a hundred fold in the coming weeks. Today we must master learn how to properly hold and use a knife and how to correctly chop an onion. Later we'll make complex dishes with many ingredients, herbs and spices!
let letters = "A";
let i = 0;
while (i < 20) {
letters += "A";
i++;
}
console.log(letters);
After coming to a conclusion, run the code and write down whether it gave you the expected result. If not, how did it differ?
Answer the following question:
Both for loops and while loops repeat code. But what are the differences? What are the similarities?
Write a for loop that will console.log()
the numbers 0 to 999.
Here is an example for
loop that prints a message 100 times:
for (let i = 0; i < 100; i++) {
console.log('Without you, today\'s emotions are the scurf of yesterday\'s');
}
What are the three components of the control statement? Each component is separated by a semicolon ;
.
Write your answers as comments in the file. Say what each part does.
- The first part of the control statement is:
- The second part of the control statement is:
- The third part of the control statement is:
Using a postfix operator i--
instead of i++
, write a for loop that iterates in reverse: console.log()
a countdown from 999 to 0.
Write a for
loop that uses interpolation to print a message to the console that includes the current value of i
.
The loop should run from 1 to 10.
Expected output in the console:
The value of i is: 1 of 10
The value of i is: 2 of 10
The value of i is: 3 of 10
The value of i is: 4 of 10
The value of i is: 5 of 10
The value of i is: 6 of 10
The value of i is: 7 of 10
The value of i is: 8 of 10
The value of i is: 9 of 10
The value of i is: 10 of 10
Congrats! You've reached the end of Section 2, and can go ahead and submit your homework. Or, if you want more, try the exercises below. If not, don't worry, you're in good shape to continue if you've made it this far, and homework submission instructrions are at the bottom.
Want an extra challenge? In many assignments or lab you'll find a "Hungry for More" section which is designed for further learning. If they seem too overwhelming you can absolutely skip them. They won't factor into your homework score, and you will be totally fine for class as long as you complete and understand the main assignment up to that point. What they will do is encourage you to go deeper, explore, and push yourself. And that will pay off big time in the long run. Below is the "hungry for more" section for this assignment.
...as long as you don't mess up the homework assignment you just did. They get more difficult/complex as you go (more or less).
- Create a directory called
test-dir
with a couple files in it. Try to make a copy of that entire directory. (Hint:man cp
for more info.) - Find and use command line shortcuts.
- Try applying one command to multiple files at once.
- Try applying one command to all files in a single directory (where necessery)
- Try applying one command to all files that match a pattern.
- Try using a mix of absolute and relative paths.
- Read about the text editor "vi" on google and see if you can use it to create and edit small javascript program (just a loop or something)
Once you've got your homework all done and pushed to github, do a pull request to get it submitted. If you're not sure how to do this, reach out to your instructor for help