Puzzling error with arrays in Makecode

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
PonderingTurtle
 
Posts: 1
Joined: Sat Jun 25, 2022 3:03 pm

Puzzling error with arrays in Makecode

Post by PonderingTurtle »

Hello, all! I've been working with our kiddo to learn programming using the Circuit Playground Express. It's been going great so far, but we have hit a bit of a snag. Earlier today, we started (trying) to use arrays and they don't seem to work the way we expected. Here is an example of what we're seeing:
error makecode.png
error makecode.png (24.21 KiB) Viewed 127 times
Note that we define the array at start, and then try to append inside a user defined function. However, the Javascript that Makecode generates is flawed:
error javascript.png
error javascript.png (7.69 KiB) Viewed 127 times
You'll note that the array is defined as a number, then assigned as an array. But in the user defined function, it doesn't know how to push -- the error the tooltip shows is "Property 'push' does not exist on type 'number'."
If in the Javascript view, I correct the code to read:

Code: Select all

function doSomething () {
    list.push(4)
}
let list = [1, 2]
The error goes away, but as soon as I pop over to Blocks mode, it breaks again.

What am I doing wrong? Any ideas? It sort of feels like a bug, but I am not confident enough in what I'm doing to say that for sure.

User avatar
DotCodesForFun
 
Posts: 5
Joined: Mon Jun 06, 2022 5:25 pm

Re: Puzzling error with arrays in Makecode

Post by DotCodesForFun »

Hi Pondering,
I think it may be because the variable list needs to be declared as an array of numbers not as a number. The JavaScript that got generated for me using the same blocks as you was:

Code: Select all

function doSomething () {
    list.push(4)
}
let list: number[] = []
list = [1, 2]

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”