0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>In the lesson about conditional constructs, we used an object with data belonging to a user called Hexlet McCoderson:</p>
1
<p>In the lesson about conditional constructs, we used an object with data belonging to a user called Hexlet McCoderson:</p>
2
<p>We used object.key when accessing the object fields to get data on the name and username of the user:</p>
2
<p>We used object.key when accessing the object fields to get data on the name and username of the user:</p>
3
<p>Let us modify the task and add the number of points and all the data to the table. It helps to display the user in the learner rankings:</p>
3
<p>Let us modify the task and add the number of points and all the data to the table. It helps to display the user in the learner rankings:</p>
4
<p>This task is easy if you need to output a single user. But what if there are hundreds of users? There are two ways:</p>
4
<p>This task is easy if you need to output a single user. But what if there are hundreds of users? There are two ways:</p>
5
<ol><li><strong>Bad</strong>: Write each user to their variable and add all users with the classic copy-and-paste method</li>
5
<ol><li><strong>Bad</strong>: Write each user to their variable and add all users with the classic copy-and-paste method</li>
6
<li><strong>Good</strong>: create an array of users with all users inside it. Every user will be allocated their place in the array, making it convenient to add or delete users</li>
6
<li><strong>Good</strong>: create an array of users with all users inside it. Every user will be allocated their place in the array, making it convenient to add or delete users</li>
7
</ol><p>We can use constructs called<strong>loops</strong>to traverse such an array. They go through each array element and get the information inside it.</p>
7
</ol><p>We can use constructs called<strong>loops</strong>to traverse such an array. They go through each array element and get the information inside it.</p>
8
<p>Passing through the elements is called<strong>iteration</strong>. In the first iteration, you'll find Hexlet McCoderson's data. In the second iteration, you'll get Layout O'Design's data.</p>
8
<p>Passing through the elements is called<strong>iteration</strong>. In the first iteration, you'll find Hexlet McCoderson's data. In the second iteration, you'll get Layout O'Design's data.</p>
9
<p>The main type of loop in Pug is the each in, meaning<em>for every an inside b</em>:</p>
9
<p>The main type of loop in Pug is the each in, meaning<em>for every an inside b</em>:</p>
10
<ul><li><strong>a</strong>is an arbitrary variable name available during iteration</li>
10
<ul><li><strong>a</strong>is an arbitrary variable name available during iteration</li>
11
<li><strong>b</strong>is the array or object you want to retrieve the data</li>
11
<li><strong>b</strong>is the array or object you want to retrieve the data</li>
12
</ul><p>We changed one line: each user in users. We read it as follows:<em>for each user in the user's array</em>and then output a tabular string with the data.</p>
12
</ul><p>We changed one line: each user in users. We read it as follows:<em>for each user in the user's array</em>and then output a tabular string with the data.</p>
13
<p>You can assign any name to the variable that will be accessible inside the loop. The name might not be user, it could be people or something else. You can choose any name that accurately represents the content.</p>
13
<p>You can assign any name to the variable that will be accessible inside the loop. The name might not be user, it could be people or something else. You can choose any name that accurately represents the content.</p>
14
<p>If the code inside the loop is large, the naming determines how quickly and accurately the code will be interpreted by you or another developer. You can read more in these articles:</p>
14
<p>If the code inside the loop is large, the naming determines how quickly and accurately the code will be interpreted by you or another developer. You can read more in these articles:</p>
15
<ul><li><a>Code Complete: Naming in Programming</a></li>
15
<ul><li><a>Code Complete: Naming in Programming</a></li>
16
<li><a>Code Complete: Naming Mistakes to Avoid in Programming I</a></li>
16
<li><a>Code Complete: Naming Mistakes to Avoid in Programming I</a></li>
17
</ul><p>You can't be sure the array we search has at least one element. The array's content won't be output If there are no elements.</p>
17
</ul><p>You can't be sure the array we search has at least one element. The array's content won't be output If there are no elements.</p>
18
<p>At this point, the user needs to know that there is no data, so they don't think there is an error with the page. It is what the `each else' is for. It is similar to the conditional construct but only works if the array is empty:</p>
18
<p>At this point, the user needs to know that there is no data, so they don't think there is an error with the page. It is what the `each else' is for. It is similar to the conditional construct but only works if the array is empty:</p>
19
<p>The code from the last example is equivalent to the following:</p>
19
<p>The code from the last example is equivalent to the following:</p>
20
<h2>Nested loops and getting the object key</h2>
20
<h2>Nested loops and getting the object key</h2>
21
<p>When going through an object, you sometimes have to get the value and the key in the object's location. For example, we can divide users into different groups: users, moderators, administrators, and so on. The object may have the following form in this case:</p>
21
<p>When going through an object, you sometimes have to get the value and the key in the object's location. For example, we can divide users into different groups: users, moderators, administrators, and so on. The object may have the following form in this case:</p>
22
<p>The users have several admin and moderator keys, which values are arrays of users. We can use a slightly modified each loop syntax to output this list with job titles. We need to get the key name and the array in it:</p>
22
<p>The users have several admin and moderator keys, which values are arrays of users. We can use a slightly modified each loop syntax to output this list with job titles. We need to get the key name and the array in it:</p>
23
<p>We take several pieces of data from the users object: the people variable gets the value, and the position variable gets the key. Pay attention to the order of the variables: the data first and then the key.</p>
23
<p>We take several pieces of data from the users object: the people variable gets the value, and the position variable gets the key. Pay attention to the order of the variables: the data first and then the key.</p>
24
<p>This example uses a nested loop which is standard for traversing big data. The first iteration in the top loop pulls the following data:</p>
24
<p>This example uses a nested loop which is standard for traversing big data. The first iteration in the top loop pulls the following data:</p>
25
<p>The next step is to search through the people array, which we do in the same way as in the example above:</p>
25
<p>The next step is to search through the people array, which we do in the same way as in the example above:</p>
26
<h2>While loop</h2>
26
<h2>While loop</h2>
27
<p>When we work with Pug, we consider the each cycle the main one, but it isn't the only one.</p>
27
<p>When we work with Pug, we consider the each cycle the main one, but it isn't the only one.</p>
28
<p>There are situations when we repeat the same actions several times. In this case, each doesn't help, but there is another loop type - while. It repeats a code section while the condition is true. For example:</p>
28
<p>There are situations when we repeat the same actions several times. In this case, each doesn't help, but there is another loop type - while. It repeats a code section while the condition is true. For example:</p>
29
<p>When we use the while, we should check that the condition changes to false sooner or later. It is a common mistake that can lead to an endless loop. In the last example, you should use count += 1; to get an infinite loop. Without it, the value of count will always be zero, meaning that the condition count < 5 will be true at any point during compilation.</p>
29
<p>When we use the while, we should check that the condition changes to false sooner or later. It is a common mistake that can lead to an endless loop. In the last example, you should use count += 1; to get an infinite loop. Without it, the value of count will always be zero, meaning that the condition count < 5 will be true at any point during compilation.</p>
30
<h2>Additional assignment</h2>
30
<h2>Additional assignment</h2>
31
<p>The following kind of array goes into the<em>icon.pug</em>:</p>
31
<p>The following kind of array goes into the<em>icon.pug</em>:</p>
32
<p>Convert the data into the following template:</p>
32
<p>Convert the data into the following template:</p>
33
33