Disclaimer and Social

This page may contain affiliate links. As an Amazon Associate I earn from qualifying purchases.

Wednesday, July 25, 2018

Use JavaScript to Decrement Loops in MakeCode

Just found out you can mix-and-match custom JavaScript code with blocks in Microsoft's MakeCode for Adafruit's Circuit Playground Express board.


At a recent maker meeting, one of the makers was trying to determine how to decrement a for-loop using the block editor inside MakeCode, but they couldn't find a way. The built-in for-loop always starts at zero, always increments by one, and always adds. So it's easy to loop from 0 to 255, but there didn't seem to be any way to count backwards from 255 back to 0.

There might be a way to use blocks to decrement a loop, but we went exploring inside the JavaScript editor instead.

Turns out, you can overwrite the blocks by editing the JavaScript code and using the traditional methods for counting down (set start at 255, change the test condition and use "--" instead of "++"). But we weren't sure how the custom code would behave - or if it would even appear in the blocks editor.

The customized code works perfectly fine, and it appears as a gray block with the custom code showing.

Custom JavaScript blocks in Micrsoft MakeCode for Adafruit Circuit Playground Express
The custom JavaScript is shown in the gray block. The code was edited in the JavaScript mode. 


This example shows how to light up the LEDs in sequence from 0 to 10, then turn them off from 10 to 0. It's a simple example, but illustrates how the code appears inside the block editor.



It's great that an old text coder like me can work comfortably in MakeCode using traditional styles, but I need to explore to see if there's an easy way to do decrementing loops in the block editor. I'm fine with text-based coding, more comfortable than with blocks honestly - but I should know more about the block editor in case I ever have to help a new programmer.

If you know how please drop some knowledge in a comment.

Also find me on:
FACEBOOK -or- INSTRUCTABLES -or- YOUTUBE

No comments:

Post a Comment