You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+32-16Lines changed: 32 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,12 +80,15 @@ for ([initialization]; [condition]; [iteration]) {
80
80
**_Usage_**: Use a `for` loop when you know how many times you want the loop to
81
81
run (for example, when you're looping through elements in an array).
82
82
83
-
####Examples
83
+
### Examples
84
84
85
85
Let's take a look at an example and get some practice using the Node debugger.
86
86
Enter the code below into the `index.js` file.
87
87
88
-
> **Note:** You can, of course, copy/paste the code rather than typing it in yourself, but we recommend typing it in for now. The act of typing code develops muscle memory and helps your brain understand and internalize the syntax.
88
+
> **Note:** You can, of course, copy/paste the code rather than typing it in
89
+
> yourself, but we recommend typing it in for now. The act of typing code
90
+
> develops muscle memory and helps your brain understand and internalize the
91
+
> syntax.
89
92
90
93
```js
91
94
for (let age =30; age <40; age++) {
@@ -115,8 +118,9 @@ loop, we increment `age` to `32`, and so on.
115
118
The **loop body** is the set of statements that we want to run when the
116
119
condition evaluates to `true`.
117
120
118
-
Let's take a look at what's happening in our loop using debugger. Run `node inspect index.js` in the terminal to start the debugger. You should see the
119
-
following:
121
+
Let's take a look at what's happening in our loop using debugger. Run
122
+
`node inspect index.js` in the terminal to start the debugger. You should see
@@ -170,13 +175,15 @@ than `gifts.length` (`3` in the above example). Our iteration, `i++`, increments
170
175
our counter by `1` at the end of each pass through the loop.
171
176
172
177
Run `node inspect index.js` in the terminal to enter the debugger, and `cont` at
173
-
the `debug` prompt to advance to the breakpoint. You should see `Wrapped teddy bear and added a bow!` logged. In our loop body, we reference `gifts[i]`. Since
174
-
`i` starts out as `0`, during the first pass through the loop `gifts[i]` is
175
-
`gifts[0]`, which is `'teddy bear'`. Continue stepping through the loop by
176
-
entering `cont` at the`debug` prompt and remember you can enter the REPL at any
177
-
point to check the values of our variables, `i` and `gifts[i]`.
178
+
the `debug` prompt to advance to the breakpoint. You should see
179
+
`Wrapped teddy bear and added a bow!` logged. In our loop body, we reference
180
+
`gifts[i]`. Since `i` starts out as `0`, during the first pass through the loop
181
+
`gifts[i]` is `gifts[0]`, which is `'teddy bear'`. Continue stepping through the
182
+
loop by entering `cont` at the`debug` prompt and remember you can enter the REPL
183
+
at any point to check the values of our variables, `i` and `gifts[i]`.
178
184
179
-
When you're done, remove the `debugger` and execute the code by running `node index.js`. You should see the following logged to the terminal:
185
+
When you're done, remove the `debugger` and execute the code by running
186
+
`node index.js`. You should see the following logged to the terminal:
180
187
181
188
```text
182
189
Wrapped teddy bear and added a bow!
@@ -191,7 +198,11 @@ of gifts and loop over them, logging our own message. Let's practice that with a
191
198
slightly different idea. To complement our gift wrapping function, your task is
192
199
to create a thank you card creator.
193
200
194
-
> **Note**: Recall the difference between logging and returning values from a function. When we log information we are simply outputting text to a terminal or console. When we return data from a function we will be able to reference and use that information elsewhere because the data is being passed out of the function.
201
+
> **Note**: Recall the difference between logging and returning values from a
202
+
> function. When we log information we are simply outputting text to a terminal
203
+
> or console. When we return data from a function we will be able to reference
204
+
> and use that information elsewhere because the data is being passed out of the
205
+
> function.
195
206
196
207
In `index.js`, build a function named `writeCards()` that accepts two arguments:
197
208
an array of string names, and an event name. Create a `for` loop with a counter
@@ -215,21 +226,26 @@ array.) The overall process should be:
215
226
Here is an example of what a call to the `writeCards()` function might look like:
If we were to call the function using this function call, it should produce the
222
233
following array as the return value:
223
234
224
235
```js
225
236
[
226
-
"Thank you, Ada, for the wonderful birthday gift!",
227
-
"Thank you, Brendan, for the wonderful birthday gift!",
237
+
"Thank you, Charlie, for the wonderful birthday gift!",
238
+
"Thank you, Samip, for the wonderful birthday gift!",
228
239
"Thank you, Ali, for the wonderful birthday gift!",
229
240
];
230
241
```
231
242
232
-
> **Top Tip**: The debugger isn't just for debugging code — you can also use it to help you write your function! Try building the structure of the loop, putting the `debugger` inside the loop body. Even before you start writing the code, you can enter the debugger's REPL and try out code until you figure out how to create the message and add it to an array. Once it's working in the REPL, transfer the code to `index.js`, exit the debugger, and run the tests.
243
+
> **Top Tip**: The debugger isn't just for debugging code — you can also use it
244
+
> to help you write your function! Try building the structure of the loop,
245
+
> putting the `debugger` inside the loop body. Even before you start writing the
246
+
> code, you can enter the debugger's REPL and try out code until you figure out
247
+
> how to create the message and add it to an array. Once it's working in the
248
+
> REPL, transfer the code to `index.js`, exit the debugger, and run the tests.
.to.deep.eq(['Thank you, Lisa, for the wonderful surprise gift!','Thank you, Kaitlin, for the wonderful surprise gift!','Thank you, Jan, for the wonderful surprise gift!']);
0 commit comments