Making Code Stick: Day 1

So this morning in my allotted study time, I got through the first three lessons of nodeschool‘s learnyounode workshop. Per yesterday’s post I did take handwritten notes and did do some experimenting to help the concepts stick.

Well, not always. Lesson one was sending “Hello World” to the console. I not only know how to do that, but used to own the developer console documentation project for Internet Explorer. I know console.log() pretty damn well by now.

Lesson 2 introduces getting arguments from the commandline and introduces the process API. Here are my written notes (transcribed, of course):

getting argument values from process.argv. What else is in the process object?

Used parseInt(), but they recommend Number() — which is better? [looked up] parseInt is vulnerable to leading zeroes in older implementations which force it to parse as octal.

parseInt parses until first non-numeric char. Number converts as the whole string. parseInt(“123a”) == “123” but Number(“123a”) == NaN.

My Code

Lesson 3 reads in a file, splits it on line endings, and counts the lines. It introduces both the file system API and importing modules. Written notes…

Needs ‘fs’ module (file system)
var fs = require(‘fs’)

fs.readFileSync(‘path/to/file’) returns buffer object that needs the .toString() method applied to output a string. **You can also provide ‘utf8’** as a second argument to force output as a string.

readFileSync is synchronous while readFile requires a callback.

Did it first with readFileSync then tried the readFile alternative for giggles.

A mistake I made was forgetting the parens on the toString method, which caused it to make the value of filecontent a function instead of the intended output of the function.

And that’s it for today. More tomorrow.

Leave a Reply

Your email address will not be published. Required fields are marked *