Skip to content

Commit eec87db

Browse files
committed
feat: add exercises for escaping strings and template literals
- Introduce new exercises on escaping strings, covering special characters and their usage. - Add a template literals exercise to demonstrate string interpolation and multi-line strings. - Update README files to provide guidance on new concepts and tasks for learners. - Remove outdated number expressions exercise and replace it with string-related tasks.
1 parent e887cbc commit eec87db

File tree

31 files changed

+207
-21
lines changed

31 files changed

+207
-21
lines changed

exercises/01.expressions-and-output/01.problem.hello-world/README.mdx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ console.log('This is a string')
1818
console.log("Double quotes work too")
1919
```
2020

21-
<callout-info>Which quotes you use is a matter of preference.</callout-info>
21+
<callout-info>
22+
Which quotes you use is a matter of preference, but I typically use single
23+
quotes. A **code formatter** is a tool that automatically formats your code to
24+
follow consistent style rules. If you have one installed (like
25+
[Prettier](https://prettier.io/)), it may automatically change your quotes
26+
when you save the file. For example, it might convert double quotes to single
27+
quotes. That's okay! The code will still work the same way.
28+
</callout-info>
2229

2330
🐨 Once you've completed the exercise, run `node index.ts` in the playground to
2431
see your output!

exercises/01.expressions-and-output/01.solution.hello-world/README.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
confused about what a value is, just log it!
77

88
🦉 Notice that strings can use either single quotes (`'`) or double quotes
9-
(`"`). In this workshop, we'll use single quotes, but both work identically.
9+
(`"`). In this workshop, we'll use single quotes, but both work almost
10+
identically.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Escaping Strings
2+
3+
👨‍💼 Sometimes you need to include special characters inside your strings. What
4+
happens if you want to include a quote character inside a string that's wrapped
5+
in quotes?
6+
7+
If you use single quotes, you need to **escape** single quotes inside the string
8+
using a backslash (`\`):
9+
10+
{/* prettier-ignore */}
11+
```ts
12+
console.log('It\'s great') // Prints: It's great
13+
```
14+
15+
And if you use double quotes, you need to escape double quotes inside the string:
16+
17+
{/* prettier-ignore */}
18+
```ts
19+
console.log("He said \"Hello\"") // Prints: He said "Hello"
20+
```
21+
22+
## Special Characters
23+
24+
The backslash also lets you include other special characters that you can't
25+
easily type:
26+
27+
| Escape Sequence | Character |
28+
| --------------- | ------------ |
29+
| `\n` | Newline |
30+
| `\t` | Tab |
31+
| `\\` | Backslash |
32+
| `\'` | Single quote |
33+
| `\"` | Double quote |
34+
35+
```ts
36+
console.log('Line 1\nLine 2') // Prints on two lines
37+
console.log('Name:\tKody') // Prints with a tab between
38+
```
39+
40+
🐨 Open <InlineFile file="index.ts" /> and complete the following tasks:
41+
42+
1. Log a string using single quotes that contains an apostrophe (like "It's
43+
working!")
44+
2. Log a string using double quotes that contains a quoted phrase (like: She
45+
said "Hi")
46+
3. Log "Hello" and "World" on separate lines using `\n` in a single string
47+
4. Log tab-separated column headers: "Name:" "Age:" "City:" using `\t`
48+
49+
💰 Remember: use `\'` to escape single quotes, `\"` to escape double quotes,
50+
`\n` for newlines, and `\t` for tabs!
51+
52+
📜 [Escape sequences on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#escape_sequences)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Escaping Strings
2+
// Learn to include special characters in strings
3+
4+
// 🐨 Log "It's working!" using single quotes (you'll need to escape the apostrophe)
5+
// 💰 Use \' to escape the apostrophe
6+
7+
// 🐨 Log: She said "Hi" (using double quotes for the string)
8+
// 💰 Use \" to escape the inner quotes
9+
10+
// 🐨 Log "Hello" and "World" on separate lines using a single string with \n
11+
// 💰 Use \n to create a newline
12+
13+
// 🐨 Log tab-separated data: Name: [tab] Age: [tab] City: (like column headers)
14+
// 💰 Use \t to create tabs between words
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "exercises_01.expressions-and-output_02.problem.escaping-strings",
3+
"type": "module",
4+
"scripts": {
5+
"start": "node index.ts",
6+
"test": "node --test index.test.ts"
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Escaping Strings
2+
3+
👨‍💼 Nice work! You've learned how to escape special characters in strings.
4+
5+
🦉 Escaping can get tedious if you have lots of special characters. The good
6+
news is that there's another way to create strings which we'll cover soon:
7+
**template literals** (using backticks). They make many of these escaping issues
8+
disappear and even let you write actual multi-line strings without `\n`!
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import assert from 'node:assert/strict'
2+
import { execSync } from 'node:child_process'
3+
import { test } from 'node:test'
4+
5+
await test('should print string with apostrophe', () => {
6+
const output = execSync('node index.ts', { encoding: 'utf8' })
7+
assert.ok(
8+
output.includes("It's working!"),
9+
'🚨 Output should include "It\'s working!" - make sure you escaped the apostrophe',
10+
)
11+
})
12+
13+
await test('should print string with quotes', () => {
14+
const output = execSync('node index.ts', { encoding: 'utf8' })
15+
assert.ok(
16+
output.includes('She said "Hi"'),
17+
'🚨 Output should include: She said "Hi" - make sure you escaped the double quotes',
18+
)
19+
})
20+
21+
await test('should print Hello and World on separate lines', () => {
22+
const output = execSync('node index.ts', { encoding: 'utf8' })
23+
assert.ok(
24+
output.includes('Hello\nWorld'),
25+
'🚨 Output should include "Hello" and "World" on separate lines - use \\n in a single string',
26+
)
27+
})
28+
29+
await test('should print tab-separated values', () => {
30+
const output = execSync('node index.ts', { encoding: 'utf8' })
31+
assert.ok(
32+
output.includes('\t'),
33+
'🚨 Output should include tab characters - make sure you used \\t for tabs',
34+
)
35+
})
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Escaping Strings
2+
// Learn to include special characters in strings
3+
4+
// prettier-ignore
5+
console.log('It\'s working!')
6+
7+
// prettier-ignore
8+
console.log("She said \"Hi\"")
9+
10+
// prettier-ignore
11+
console.log('Hello\nWorld')
12+
13+
// prettier-ignore
14+
console.log('Name:\tAge:\tCity:')
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "exercises_01.expressions-and-output_02.solution.escaping-strings",
3+
"type": "module",
4+
"scripts": {
5+
"start": "node index.ts",
6+
"test": "node --test index.test.ts"
7+
}
8+
}

exercises/01.expressions-and-output/03.problem.numbers/index.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)