Summary - Strings
- Node.js course
- Beginner
- Lesson 35/116
- 5 min
Combine string methods to write a business card.
Business card app
In this summary we will combine the string methods you learned to print a business card. The target is a three-line card: name (upper-cased), phone (fixed width) and email (whitespace trimmed).
The flow is: name.toUpperCase() upper-cases the name, phone.padStart(10, "0") shows the phone at a fixed width, email.trim() removes side whitespace. We then print each line with console.log. The business card is a scenario where string methods do real work.
Practice Print the business card
The name, phone and email variables are ready. Print a three-line card: upper-case the name with name.toUpperCase(), fix the phone with phone.padStart(10, "0") and trim the email with email.trim(). Print each line with console.log. The output must be three lines: ALEX, 0005551234 and alex@x.com.
Starter code
const name = "Alex";
const phone = "5551234";
const email = " alex@x.com ";
// Print the three lines in orderSample solution
const name = "Alex";
const phone = "5551234";
const email = " alex@x.com ";
console.log(name.toUpperCase());
console.log(phone.padStart(10, "0"));
console.log(email.trim());Expected output
ALEX 0005551234 alex@x.com
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which method upper-cases the name on the card?
name.???- .toLowerCase()
- .trim()
- .padStart(3, " ")
- .toUpperCase() Correct answer
Explanation: toUpperCase upper-cases the letters; the name stands out on the card.
2 Write the phone line
const phone = "5551234";
const line = phone____(10, "0");
const phone = "5551234";
const line = phone.padStart(10, "0");
Explanation: padStart fixes the phone to 10 characters.
3 Match each method to its job
| Term | Meaning |
|---|---|
toUpperCase |
Upper-cases name |
padStart |
Fixes phone width |
trim |
Removes whitespace |
split |
Breaks list into array |