Driver's License Code-Along Activity
In this activity, build a Node.js application that displays driver's license information using template literals. This activity covers:
- An Introduction to Node.js
- A Review of Functions and Conditional Statements
- An Introduction to Template Literals
Setting Up a Node.js App
Node.js is a JavaScript Runtime. It allows developers to write JavaScript that runs on a server, instead of in a web browser. Node.js is mostly used to build network applications (like web servers), but it can also be used to create console apps!
Getting Started
First, build the most basic possible Node.js app: an app that displays a message.
- Log into repl.it
- Create a new Node.js project
- Name it "Driver's License App"
- Open the index.js file
- At the top of the file, add the following code:
console.log("Welcome to the Driver's License App!");
- Click the "Run" button to see the message appear in the console!
A Driver's License
Next, add some code to display someone's driver's license. It should end up looking something like this in the console:
/////////////////////
| Name: Marge Simpson
| Age: 36
| Donor: true
/////////////////////
- Under the
console.log
, create a new variable namedlicenseStr
- Use
let
instead ofvar
- Use
- Set it equal to
'/////////////////////\n'
- Add a
+
at the end of that line, and make a new line - On the next line, add the following:
'| Name: Marge Simpson\n'
- Continue adding each new line as part of the
licenseStr
variable - Log the
licenseStr
variable to the console
Run the program, and verify that the license appears!
console.log("Welcome to the Driver's License App!");
let licenseStr = '/////////////////////\n' +
'| Name: Marge Simpson\n' +
'| Age: 36\n' +
'| Donor: true\n' +
'/////////////////////\n';
console.log(licenseStr);
A Function
The functionality of the program is good so far, but it would be much more extendable if the code were in a function.
- At the top of the file, define a new function named
printLicense
function printLicense() { }
- Copy the printing code into the body of the
printLicense
function - Remove the printing code from its previous location
- Call the
printLicense
function under its definitionprintLicense()
Run the program, and verify that it still prints the license!
function printLicense() {
let licenseStr = '/////////////////////\n' +
'| Name: Marge Simpson\n' +
'| Age: 36\n' +
'| Donor: true\n' +
'/////////////////////\n';
console.log(licenseStr);
}
printLicense();
A Function with Parameters
Now that the printing code is within a function, it will be possible to make it more dynamic. Instead of always printing information for Marge Simpson, print the information from some parameters.
- Add three parameters to the
printLicense
function definitionname
,age
, anddonor
- Update the
printLicense
call, and pass in three arguments"Marge Simpson", 36, true
- In the body of the
printLicense
function, findMarge Simpson
- Remove the specific name, and replace it with the
name
parameter- Concatenate the
'| Name: '
with thename
, and then the'\n'
- Concatenate the
- Replace the other values in the string in the same way
- Find the call to the
printLicense
function - Under that, call the
printLicense
function again, passing in new values"Bart Simpson", 10, false
Run the program, and verify that both driver's licenses appear!
function printLicense(name, age, donor) {
let licenseStr = '/////////////////////\n' +
'| Name: ' + name + '\n' +
'| Age: ' + age + '\n' +
'| Donor: ' + donor + '\n' +
'/////////////////////\n';
console.log(licenseStr);
}
printLicense('Marge Simpson', 36, true);
printLicense('Bart Simpson', 10, false);
Aside: Conditional Console Colors
When working with Node.js, it is possible to change the color of the text printed to the console. Check out this Stack Overflow answer for more information.
Update the code so that people older than 16
will have green licenses, and other people will have red licenses.
- Make a new line at the top of the body of the
printLicense
function - There, create an
if
statement checking ifage
is more than16
- In the body of the
if
, log\x1b[32m
to the console- This changes whatever text comes next to green
- After the
if
body, add anelse { }
- In the body of the
else
, log\x1b[31m
to the console- This changes whatever text comes next to red
Run the program, and verify that the colors of the printed licenses change based on the age!
if (age > 16) {
console.log('\x1b[32m');
} else {
console.log('\x1b[31m');
}
Template Literals
Now it's finally time to learn about template literals! Looking at the string printing code right now, it's a little messy. Using template literals can help clean them up a lot.
Template Literals are a lot like normal strings, but instead of quotes or double quotes, they use backticks (`). They also allow for multi-line strings, and string interpolation. Replace the existing strings with template strings to see how much simpler they make things!
Interpolation
The first step is to use interpolation. This allows developers to easily place expressions or values within strings, without having to use +
to concatenate multiple things together! Imagine an example like this:
let elevation = 1000;
let temp = 5;
console.log('The elevation is ' + elevation + 'ft and the temperature is ' + temp + ' degrees!');
All of those +
and '
can become confusing pretty quickly, especially with multiple values. Using template literals, it would look like this:
console.log(`The elevation is ${elevation}ft and the temperature is ${temp} degrees!`);
Interpolated values begin with ${
and end with }
. Everything within will be dynamic, based on the values of the expressions.
Update the code so that it uses template literals instead of normal strings!
- Replace the quotes (
'
) with backticks (`) for each line - Remove the
+
and extra strings, so each line is back to being one string - Use
${name}
to insert thename
value - Do the same for
${age}
and${donor}
Run the program, and verify that it works as expected!
let licenseStr = `/////////////////////\n` +
`| Name: ${name}\n` +
`| Age: ${age}\n` +
`| Donor: ${donor}\n` +
`/////////////////////\n`;
Multi-line
The other big benefit of template literals is the ability to create multiline strings. Instead of appending all of those \n
new line characters, the new lines persist! For example:
console.log(`Here is
some text`);
prints:
Here is
some text
Watch out for extra whitespace though! Since the whitespace matters, any extra indentation or tabs within the backticks will persist.
Update the code so that it uses one template literal that spans multiple lines!
- Remove all backticks except the first and last
- Move the first string line to its own line
- Remove all the
+
concatenationss - Remove all the
\n
characters
Run the program again, and verify that it still works as expected!
let licenseStr = `
/////////////////////
| Name: ${name}
| Age: ${age}
| Donor: ${organDonor}
////////////////////////////
`;
Ultimately, using template literals makes things much easier to scale and maintain. They will come in handy when working with strings in almost any situation!
Final Code
console.log("Welcome to the Driver's License App!");
function printLicense(name, age, organDonor) {
if (age > 16) {
console.log('\x1b[32m');
} else {
console.log('\x1b[31m');
}
let licenseStr = `
/////////////////////
| Name: ${name}
| Age: ${age}
| Donor: ${organDonor}
////////////////////////////
`;
console.log(licenseStr);
}
printLicense('Marge Simpson', 36, true);
printLicense('Bart Simpson', 10, false);