Sample Video Frame
10: Files, Args, and Variables
In this exercise you are going to use everything you know so far to create a larger script. I've started you off with a few lines of code that uses most of what you have learned so far. Your job is to take this code as far as you can in several ways:
- Add more to it, possibly from things you found in the file API from Exercise 9.
- Break it in creative ways.
- Make it more resistant to breaking.
You should attempt to do this yourself, and when you run out of ideas just delete random characters.
The Code
// Exercise 10: Files, Args, Variables, Oh My
const fs = require('fs');
let file_to_open = process.argv[2];
let file_contents = fs.readFileSync(file_to_open);
console.log(`The file named ${file_to_open} contains:`);
console.log(file_contents.toString());
Remember to try your best and don't worry if you can't think of too many things you can do right now. The purpose of these small challenges is to help you jump ahead in tiny increments at key points in the course.
What You Should See
If I run the code that I have here on our little test file from Exercise 9 I get this:
The file named test.txt contains:
I have a problem.
It's not a problem with you.
It's a problem with sleep.
And you have it too.
Register for Learn JavaScript the Hard Way
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.