Skip to content

Commit

Permalink
use template literals for assertions (#326)
Browse files Browse the repository at this point in the history
* use a template literal

* add original expression back to the first assertion

* fix broken assertion

* update assertion call site

---------

Co-authored-by: Dedekind561 <[email protected]>
  • Loading branch information
Dedekind561 and Dedekind561 authored Oct 22, 2023
1 parent 8d08c64 commit 38320ed
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions content/js1/blocks/assertions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ facilitation = false
emoji= '🧩'
[objectives]
1='Describe the difference between console.log and console.assert'
2 = 'Write an assertion to check for some behaviour of a given function'
2='Write an assertion to check for some behaviour of a given function'
3='Implement a piece of functionality specified in an assertion'
4='Given an assertion and a function declaration, identify and explain whether the assertion will succeed or fail'
5 ='Write an assertion for a function that implements a solution to a given problem'
Expand Down Expand Up @@ -65,29 +65,25 @@ Predict and explain if the assertion will succeed or fail. Pay particular attent

### Clarity with arguments

It would be useful to have more information as to why this assertion failed. We can pass additional arguments to `console.assert`:
It would be useful to have more information as to why this assertion failed. We can pass an additional argument to `console.assert`:

```js
function formatAs12HourClock() {}

console.assert(
formatAs12HourClock("08:00") === "08:00 am",
"current function output: %s, target output: %s",
formatAs12HourClock("08:00"),
"08:00 am"
`current output: ${formatAs12HourClock("08:00")}, target output: 08:00 am`
);
```

Let's break down these arguments to make sense of what's going on:

1. **first argument** - `formatAs12HourClock("08:00") === "08:00 am"` - the condition we're checking
2. **second argument** - `"current function output: %s, target output: %s"` - a message string that will be logged to the console if the condition is false.
3. **third argument** - `formatAs12HourClock("08:00")` - this value will be substituted into the message string at the first "%s"
4. **fourth argument** - `"20:00"` - this value will be substituted into the message string at the second "%s"
2. **second argument** - `current output: ${formatAs12HourClock("08:00")}, target output: 08:00 am` - a message string that will be logged to the console if the condition is false.

#### 🧹 Refactor

We can tidy up the assertion even further. As we’re repeating the same expressions, we can store their result in variables with meaningful names so we can reuse them:
We can tidy up the assertion even further. As we’re reusing the same expressions, we can store their result in variables with meaningful names so we can reuse them:

```js {linenos=table,linenostart=1}
function formatAs12HourClock() {}
Expand All @@ -96,9 +92,7 @@ const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
currentOutput === targetOutput,
"current output: %s, target output: %s",
currentOutput,
targetOutput
`current output: ${currentOutput}, target output: ${targetOutput}`
);
```

Expand Down Expand Up @@ -129,9 +123,7 @@ const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
currentOutput === targetOutput,
"current output: %s, target output: %s",
currentOutput,
targetOutput
`current output: ${currentOutput}, target output: ${targetOutput}`
);
```

Expand All @@ -156,18 +148,14 @@ const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
currentOutput === targetOutput,
"current output: %s, target output: %s",
currentOutput,
targetOutput
`current output: ${currentOutput}, target output: ${targetOutput}`
);

const currentOutput = formatAs12HourClock("23:00");
const targetOutput = "11:00 pm";
console.assert(
currentOutput === targetOutput,
"current output: %s, target output: %s",
currentOutput,
targetOutput
`current output: ${currentOutput}, target output: ${targetOutput}`
);
```

Expand Down Expand Up @@ -198,19 +186,15 @@ const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
currentOutput === targetOutput,
"current output: %s, target output: %s",
currentOutput,
targetOutput
`current output: ${currentOutput}, target output: ${targetOutput}`
);

// ❌ this assertion now fails
const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
"current output: %s, target output: %s",
currentOutput2,
targetOutput2
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
```

Expand Down

0 comments on commit 38320ed

Please sign in to comment.