This week we took a closer look at one of the most useful commands in javascript – the for loop!. We first used a for loop to first generate a grid of circles:
Next, we made a slight change to get the for loop to display rectangles and circles in form that would make any Mayo supporter teary-eyed:
Finally, we used the for loop to recreate a classic 1980’s basic program to javascript. This uses a trick of picking random forward or backward slashes to automatically generate a maze:
We then spent the rest of the class experimenting with different colors, animations, strokes, framerates, probabilities, etc to get some really interesting effects (often by accident!).
For anyone that’s interested, the classic program is below and it was amazing in that it did it in a single line on a commodore 64!:
10 PRINT CHR$(205.5+RND(1)); : GOTO 10
There is a whole book out there on this line of code! It’s worth flicking through as it shows lots of other maze variants that are possible with some tweaking.
For a good explanation of the for loop – head over the W3C Schools Javascript site – it does quite a good job on explaining it, or else this tutorial from digitalocean is also good.
As usual, all the code is up on github! Happy creating!!
The book you’ve shared looks interesting, Mark.
I’m especially drawn to the section dedicated to Processing, since it’s an IDE I’m learning to use these days.
Just what I needed!😊
A pleasure – yes processing is very similar to the p5 javascript framework we use in the creators group.