See the Installfest project for more instructions.
Software:
Accounts:
If you do not have these, RAISE YOUR HAND!
You may never have coded before. Today you will.
In this class, you will learn about:
Follow along online! Put a browser pointed at this site on one side of your screen, and Terminal on the other.
The best way to learn is to teach.
- Latin proverb
Generally, code is something that stands for something else.
Specifically, source code is a series of instructions that tell a computer what to do.
With computers, "code" is not about secrets -- it's about symbols.
"The only perfect program is an empty file." - Alex
When you are coding, you are not baking cookies; you are writing a recipe for how to make cookies.
Writing a recipe involves trying out the recipe (baking a test batch), then tweaking the recipe and trying again and again until you get it right.
(recipe from popcornpottery.com)
If your code is a two-year-old child, then an error is a temper tantrum.
(It can take effort to figure out the underlying reason why they're upset and fix it.)
See also: What went wrong? from MDN
For example:
<button onclick='clicked()'>Click Me!</button>
<script>
let clicks = 0;
function clicked() {
clicks = clicks + 1;
event.target.textContent = clicks;
}
</script>
Computers used to not have screens! They were connected to devices like this:
The Terminal app is a direct descendant of a TeleType printer or TTY.
When you type into the console and hit Enter you are pretending to type a line onto a TTY; the scrolling terminal is like a roll of printer paper.
See this twitter thread for more history and TTY pix.
picture of Teletype Corporation ASR-33 on display at the Computer History Museum by ArnoldReinhold [CC BY-SA 3.0] via Wikimedia
to open your Terminal:
{bash::bash}
C:\Users\yourname
cmd.exe
or PowerShell
) -- but beware, the commands are slightly different than in bashImportant: make your terminal as tall as possible and don't overlap windows
$
or >
symbolnode
-- that's you commanding the computer to launch node
>
prompt1 + 1
2
An "engine" is a type of program that either executes or empowers other programs.
NodeJS (aka node
) is an engine that runs JavaScript programs -- either from files, or interactively from the command line.
WARNING: Before you start typing, look at the prompt!
Davids-Macbook-Pro:~ David$
(Mac)david@davidspc:~$
(Ubuntu Linux)C:\Users\david>
(Windows)>
characterFrom inside node
, if you want to get back to the shell...
.exit
and Enter
pwd
("print working dir") -- shows the name of the current directoryls
("list") -- shows the contents of the current directorymkdir
("make dir") -- creates a new subdirectory inside the current directorycd
("change dir") -- move into a different directoryWindows shells have slightly different commands; if these commands don't work for you, ask a TA how to use "bash" instead.
pwd
code
directory inside your home directorycode
for each projectmkdir code
cd code
pwd
cd
ls
(and note that it's empty)
dir
node
and then the name of the source file, like this:$ node hello.js
Hello, World!
code
subdirectory using pwd
and/or cd
hello.js
using the File > New menuInside this file, put the following source code:
console.log("Hello, World!");
Save the file
Switch back to the terminal (using Alt-Tab or Cmd-Tab or clicking)
(If you are using VS Code, you can click Terminal → New Terminal for the built-in terminal panel)
Run this file using node hello.js
What happens? Is this what you expected?
code
directory, create a file named countdown.js
Inside this file, put the following source code:
let count = 10;
while (count > 0) {
console.log(count + '...');
count = count - 1;
}
console.log('Blastoff!');
Save the file
In your terminal, run node countdown.js
Change line 1 to the following:
let count = parseInt(process.argv[2])
run the program with new inputs from the command line like this:
node countdown 5
node countdown 100
process
is a built-in library object provided by NodeJSprocess.argv
is an array containing the command-line arguments
node countdown 99
, argv
contains...index | meaning | value |
---|---|---|
0 | location of the NodeJS engine | '/usr/local/Cellar/node/11.10.0/bin/node' |
1 | location of the current program | '/Users/alex/code/countdown' |
2 | "first" command-line argument | '99' |
process.argv[2]
gets the third item from the array process.argv
/