![]() ![]() After these variables are defined, the p5.js setup() function is called to create the canvas and draw the board. Information on getting started with P5.js can be found here: įor the example below, first variables are established to handle the square size, number of squares, border size and canvas size. To bring this logic into a library, we first need to understand how to work with the library itself. Defining the variables for size and text content.Īnd it results in the following console text: Creating a function to contain the chessboard. Using the nested the nested for loop to create a console chess board can be accomplished as follows: Times in between each hi, it could look like this: Īnd the console output would look like this: So if we wanted to say “hi” 5 times and “wait” 3 In our example above, if we placed a for loop in the statement section it would be run between each time the console.log('Hi '+i) was run. Nesting one for loop within another allows for another for loop to be run atĮvery step of the loop it is inside of. The Eloquent JavaScript Chess Board exercise calls for the creation of a script that creates a chess board like graphic in the console by using nested for loops. This is produce the following in the console: Loop counter value to the console, it could look like this: ![]() If we added a console.log() function to the statement section that prints Hi and the current Is less than 10, the statement section is executed and finally the counter is increased by 1 (that is what the ++ does). So, the above loop creates a counter that starts at 0, if the counter In the above, let 1 = 0 is the initialization expression, i is the conditional expression, and i++ is the increment expression. Īfter the parenthesis, there is a set of curly braces, the code to be executed during each loop is placed within these braces. The increment expression, this updates the loop counter and is executed after the statement section each loop. The conditional expression, this checks a true/false condition, if true the code within the statement section executes. The initialization expression, this sets the loop counter. Nested For Loop in Three.js (3D Chess Board) Ī for loop has 3 parts, these parts are separated by semicolons and contained within a set of parenthesis ( ). Nested For Loop in P5.js (2D Chess Board) Nested For Loop (Eloquent JavaScript Chess Board Example) This page outlines the use of for loops and nested for loops starting with vanilla JavaScript and then extends the logic to examples in the P5.js and Three.js JavaScriptĬhess Board exercise part of Chapter 2 of the book Eloquent JavaScript.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |