Skip to content

Commit

Permalink
convert problem statements from txt to markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Dec 25, 2013
1 parent 52d84dc commit 9e8188c
Show file tree
Hide file tree
Showing 24 changed files with 439 additions and 583 deletions.
32 changes: 32 additions & 0 deletions problems/baby_steps/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Write a program that accepts one or more numbers as command-line arguments and prints the sum of those numbers to the console (stdout).

----------------------------------------------------------------------
## HINTS

You can access command-line arguments via the global `process` object. The `process` object has an `argv` property which is an array containing the complete command-line. i.e. `process.argv`.

To get started, write a program that simply contains:

```js
console.log(process.argv)
```

Run it with `node myprogram.js` and some numbers as arguments. e.g:

```sh
$ node myprogram.js 1 2 3
```

In which case the output would be an array looking something like:

```js
[ 'node', '/path/to/your/program.js', '1', '2', '3' ]
```

You'll need to think about how to loop through the number arguments so you can output just their sum. The first element of the process.argv array is always 'node', and the second element is always the path to your program.js file, so you need to start at the 3rd element (index 2), adding each item to the total until you reach the end of the array.

Also be aware that all elements of `process.argv` are strings and you may need to *coerce* them into numbers. You can do this by prefixing the property with `+` or passing it to `Number()`. e.g. `+process.argv[2]` or `Number(process.argv[2])`.

{appname} will be supplying arguments to your program when you run `{appname} verify program.js` so you don't need to supply them yourself. To test your program without verifying it, you can invoke it with `{appname} run program.js`. When you use `run`, you are invoking the test environment that {appname} sets up for each exercise.

----------------------------------------------------------------------
42 changes: 0 additions & 42 deletions problems/baby_steps/problem.txt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
Create a program that prints a list of files in a given directory,
filtered by the extension of the files. You will be provided a
directory name as the first argument to your program (e.g.
'/path/to/dir/') and a file extension to filter by as the second
argument.
Create a program that prints a list of files in a given directory, filtered by the extension of the files. You will be provided a directory name as the first argument to your program (e.g. '/path/to/dir/') and a file extension to filter by as the second argument.

For example, if you get 'txt' as the second argument then you will
need to filter the list to only files that {bold}end with .txt{/bold}.
For example, if you get 'txt' as the second argument then you will need to filter the list to only files that **end with .txt**.

The list of files should be printed to the console, one file per line.
You {bold}must{/bold} use asynchronous I/O.
The list of files should be printed to the console, one file per line. You **must** use asynchronous I/O.

----------------------------------------------------------------------
HINTS:
## HINTS

The `fs.readdir()` method takes a pathname as its first argument and a
callback as its second. The callback signature is:
The `fs.readdir()` method takes a pathname as its first argument and a callback as its second. The callback signature is:

function (err, list) { ... }
```js
function callback (err, list) { /* ... */ }
```

where `list` is an array of filename strings.

Documentation on the `fs` module can be found by pointing your browser
here:
Documentation on the `fs` module can be found by pointing your browser here:
{rootdir:/node_apidoc/fs.html}

You may also find node's `path` module helpful, particularly the
`extname` method.
You may also find node's `path` module helpful, particularly the `extname` method.

Documentation on the `path` module can be found by pointing your
browser here:
Documentation on the `path` module can be found by pointing your browser here:
{rootdir:/node_apidoc/path.html}

----------------------------------------------------------------------
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
Write a program that prints the text "HELLO WORLD" to the console
(stdout).
Write a program that prints the text "HELLO WORLD" to the console (stdout).

----------------------------------------------------------------------
HINTS:
## HINTS

To make Node.js program, create a new file with a `.js` extension and
start writing JavaScript! Execute your program by running it with the
To make Node.js program, create a new file with a `.js` extension and start writing JavaScript! Execute your program by running it with the
`node` command. e.g.:

$ node myprogram.js
```sh
$ node myprogram.js
```

You can write to the console in the same way as in the browser:

console.log("text")

```js
console.log("text")
```

When you are done, you must run:

{appname} verify myprogram.js
```sh
$ {appname} verify myprogram.js
```

to proceed. Your program will be tested, a report will be generated,
and the lesson will be marked 'completed' if you are successful.
to proceed. Your program will be tested, a report will be generated, and the lesson will be marked 'completed' if you are successful.

----------------------------------------------------------------------
29 changes: 29 additions & 0 deletions problems/http_client/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Write a program that performs an HTTP GET request to a URL provided to you as the first command-line argument. Write the String contents of **each** "data" event from the response to a new line on the console (stdout).

----------------------------------------------------------------------
## HINTS

For this exercise you will need to use the `http` core module.

Documentation on the `http` module can be found by pointing your browser here:
{rootdir:/node_apidoc/http.html}

The `http.get()` method is a shortcut for simple GET requests, use it to simplify your solution. The first argument to `http.get()` can be the URL you want to GET, provide a callback as the second argument.

Unlike other callback functions, this one has the signature:

```js
function callback (response) { /* ... */ }
```

Where the `response` object is a Node **Stream** object. You can treat Node Streams as objects that emit events, the three events that are of most interest are: "data", "error" and "end". You listen to an event like so:

```js
stream.on("data", function (data) { /* ... */ })
```

The "data" is emitted when a chunk of data is available and can be processed. The size of the chunk depends upon the underlying data source.

The `response` object / Stream that you get from `http.get()` also has a `setEncoding()` method. If you call this method with "utf8", the "data" events will emit Strings rather than the standard Node `Buffer` objects which you have to explicitly convert to Strings.

----------------------------------------------------------------------
39 changes: 0 additions & 39 deletions problems/http_client/problem.txt

This file was deleted.

51 changes: 51 additions & 0 deletions problems/http_collect/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Write a program that performs an HTTP GET request to a URL provided to you as the first command-line argument. Collect **all** data from the server (not just the first "data" event) and then write two lines to the console (stdout).

The first line you write should just be an integer representing the number of characters received from the server and the second line should contain the complete String of characters sent by the server.

----------------------------------------------------------------------
## HINTS

There are two approaches you can take to this problem:

**1)** Collect data across multiple "data" events and append the results together prior to printing the output. Use the "end" event to determine when the stream is finished and you can write the output.

**2)** Use a third-party package to abstract the difficulties involved in collecting an entire stream of data. Two different packages provide a useful API for solving this problem (there are likely more!): `bl` (Buffer List) and `concat-stream`; take your pick!

http://npm.im/bl
http://npm.im/concat-stream

To install a Node package, use the Node Package Manager `npm`. Simply type:

```sh
$ npm install bl
```

And it will download and install the latest version of the package into a subdirectory named `node_modules`. Any package in this subdirectory under your main program file can be loaded with the `require` syntax without being prefixed by './':

```js
var bl = require('bl')
```

Node will first look in the core modules and then in the `node_modules` directory where the package is located.

If you don't have an Internet connection, simply make a `node_modules` directory and copy the entire directory for the package you want to use from inside the {appname} installation directory:

{rootdir:/node_modules/bl}
{rootdir:/node_modules/concat-stream}

Both `bl` and `concat-stream` can have a stream *piped* in to them and they will collect the data for you. Once the stream has ended, a callback will be fired with the data:

```js
response.pipe(bl(function (err, data) { /* ... */ }))
// or
response.pipe(concatStream(function (data) { /* ... */ }))
```

Note that you will probably need to `data.toString()` to convert from a Buffer.

Documentation for both of these modules has been installed along with {appname} on your system and you can read them by pointing your browser here:

{rootdir:/docs/bl.html}
{rootdir:/docs/concat-stream.html}

----------------------------------------------------------------------
67 changes: 0 additions & 67 deletions problems/http_collect/problem.txt

This file was deleted.

39 changes: 39 additions & 0 deletions problems/http_file_server/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Write an HTTP **server** that serves the same text file for each request it receives.

Your server should listen on the port provided by the first argument to your program.

You will be provided with the location of the file to serve as the second command-line argument. You **must** use the `fs.createReadStream()` method to stream the file contents to the response.

----------------------------------------------------------------------
## HINTS

Because we need to create an HTTP server for this exercise rather than a generic TCP server, we should use the `http` module from Node core. Like the `net` module, `http` also has a method named `http.createServer()` but this one creates a server that can talk HTTP.

`http.createServer()` takes a callback that is called once for each connection received by your server. The callback function has the signature:

```js
function callback (request, response) { /* ... */ }
```

Where the two arguments are objects representing the HTTP request and the corresponding response for this request. `request` is used to fetch properties, such as the header and query-string from the request while `response` is for sending data to the client, both headers and body.

Both `request` and `response` are also Node streams! Which means that you can use the streaming abstractions to send and receive data if they suit your use-case.

`http.createServer()` also returns an instance of your `server`. You must call `server.listen(portNumber)` to start listening on a particular port.

A typical Node HTTP server looks like this:

```js
var http = require('http')
var server = http.createServer(function (req, res) {
// request handling logic...
})
server.listen(8000)
```

Documentation on the `http` module can be found by pointing your browser here:
{rootdir:/node_apidoc/http.html}

The `fs` core module also has some streaming APIs for files. You will need to use the `fs.createReadStream()` method to create a stream representing the file you are given as a command-line argument. The method returns a stream object which you can use `src.pipe(dst)` to pipe the data from the `src` stream to the `dst` stream. In this way you can connect a filesystem stream with an HTTP response stream.

----------------------------------------------------------------------
Loading

0 comments on commit 9e8188c

Please sign in to comment.