Sample Video Frame

12: Functions, Files, Variables

We are now going to practice the functions you just learned about by combining them with files and using variables, both in your JavaScript file and inside the functions. Just as with earlier challenges, your job in this is to take the small amount of code I've started here and expand on it in as many weird and clever ways as you can. To get your creative juices going, I've written this poem of random words to use:

The living light I once
could have progressed to vermilion
and concerned a compact miniscule
but no, that wasn't
it was this once in a million
terrible ways of broken chance

Put that in a file, or write your own sequence of random lines to play with in your JavaScript file.

The Code

// Exercise 12: Functions, Files, Variables 

const fs = require('fs');

const print_lines = (err, data) => {
    console.log(data.toString());
}

const yell_at_me = (what) => {
    return what.toUpperCase();
}

fs.readFile("poem.txt", print_lines);

// let's do that again but with an anonymous function
// you've actually seen this before

fs.readFile("poem.txt", (err, data) => {
    let yelling = yell_at_me(data.toString());
    print_lines(err, yelling);
});

In this code you print the poem twice. First you print it using a separate function called print_lines. Then you print it again but in a more complex way that does this:

  1. It uses an anonymous callback function at the end of fs.readFile.
  2. Inside this callback you call another function called yell_at_me, which uppercases the poem and returns it (see line 10).
  3. It then prints this yelling version of the poem using print_lines.

What You Should See

$ node "code.js" poem.txt
The living light I once
could have progressed to vermilion
and concerned a compact miniscule
but no, that wasn't
it was this once in a million
terrible ways of broken chance


THE LIVING LIGHT I ONCE
COULD HAVE PROGRESSED TO VERMILION
AND CONCERNED A COMPACT MINISCULE
BUT NO, THAT WASN'T
IT WAS THIS ONCE IN A MILLION
TERRIBLE WAYS OF BROKEN CHANCE

You've Been Doing Function Calls

I couldn't explain something you've been doing until you learned about functions. Remember the code from Exercise 1?

console.log("Hi I am something you did before.");

While you have been working on this book I've mostly just said type that in and I'll explain it later. Now I can explain it. The console.log() is actually a function that is already defined for you. It's defined a little differently from your functions because it is inside "something" called console, but you'll learn about that part later. For now, you just need to know that things like console, fs, and readline-sync are containers for groups of functions like log.

When you use these functions they work exactly like the ones you just made. They have a list of parameters, they do something with those parameters, and then they might return a value or modify the parameters you give them. To be sure what they do you would need to look up their documentation and read it. Any time you're not sure what something is, you can search online for it with "javascript X" where X might be console.log. Searching for "node console.log" would pull up the Node.js-specific documentation, which is most likely more useful to you since you are using node.

Extra Challenge

You should take the time to go back through exercises 1-12 and look at each function call and see if you can write the same kind of function like this:

const myLog = (toprint) => {
    console.log(toprint);
}

Just practice "wrapping" other functions and trying to match how they are defined. This will help you figure out how to make these kinds of functions yourself. In my preceding example I don't really do a good job of replicating how console.log is defined, so maybe you can do better?


Learn JavaScript Today

Register today for the Early Access course and get the all currently available videos and lessons, plus all future modules for no extra charge.


Still Not Sure? Try the next FREE Lesson!