TypeError: blocks.append is not a function

URL of experiment: https://pavlovia.org/Wake/prospective-memory-ldt

Description of the problem:

My python code is:

blocks = [["Practice",10,0,"The following block contains 10 practice trials. Please press the 'N' key when a non word is presented, press the 'W' key when a word is presented."]]
pmblocks = [["Focal",95,5,"Instruct Focal"],["Non-Focal",95,5,"Instruct Non-Focal"]]
shuffle(pmblocks)
blocks.append(pmblocks[0])
blocks.append(pmblocks[1])

The Auto->JS gives

blocks = [["Practice", 10, 0, "The following block contains 10 practice trials. Please press the 'N' key when a non word is presented, press the 'W' key when a word is presented."]];
pmblocks = [["Focal", 95, 5, "Instruct Focal"], ["Non-Focal", 95, 5, "Instruct Non-Focal"]];
shuffle(pmblocks);
blocks.append(pmblocks[0]);
blocks.append(pmblocks[1]);

This gives an error TypeError: blocks.append is not a function
I’ve used .append earlier in the experiment, so if this error is the first one it encounters then it must be something specific about this way of using it.

Best wishes,

Wakefield

1 Like

@wakecarter, looks like an auto-translation error, since append is not an array method in JS. The equivalent in JS is the push method. @jon will know a bit more about how the auto-translate features will be improved in future but for now, I would suggest some post translation editing, by setting the code type to “Both” after entering the Python code.

Thanks David,

I’ve just started a crib sheet in Google docs to collate code that needs to be translated manually. If anyone has anything to add, please add a comment to the document. Otherwise it will grow organically as I translate my own code.

Best wishes,

Wakefield

2 Likes

Thanks Wakefield. Hopefully Alain can gradually address these in the code translator :slight_smile:

@wakecarter, a solution that allows append to be used in the code components is to add the Array push method to the Array prototype / base class in the Begin Experiment code tab.

Array.prototype.append = [].push
1 Like

That’s great news and will save me a lot of frustration (either repeatedly changing array to push or forgetting that my code component is now both not auto)

I am having hard time using this code

Array.prototype.append = [].push

Instead of python append function. In the code component if I add this to Begin Experiment* , I should set it to both first and then add this code to js section ? Then later on in js section can i use append or do I use push() ?

As per my crib sheet I strongly recommend having a JS only code component called code_JS in your first routine where you place functions and definitions like this (copy and pasted from my crib sheet ÂŁ. This then allows you to use .append and other Python functions in Auto translation components later

Thanks @wakecarter so I exactly followed you cribsheet direction and created the js code routine right in the beginning.

I have this function :

if (CircleBig_mousepractice.contains(practice_mouse) && practice_mouse.getPressed()[0] === 1 ){
 crosses.append(makeCross(practice_mouse.getPos()));
}

This is throwing me error although the function is in js.

Take another look at the crib sheet, this issue is how you are defining your functions. To give the function global scope, use:

makeCross = function(pos) {

}

Hi, so I created a new js only code component to contain my makeCross function.

makeCross = function makeCross(pos) {
    var cross;
    cross = new visual.ShapeStim(win, {"pos": pos, "depth": (- 2), "vertices": "cross", "size": [0.05, 0.05], "lineColor": [(- 0.8), (- 0.8), 1], "fillColor": [(- 0.8), (- 0.8), 1]});
    cross.setAutoDraw(true);
    return cross;
}

This didn’t work. Does it look correct ?

David said

makeCross = function(pos) {

}

You wrote

makeCross = function makeCross(pos) {
   
}
1 Like

Oh wow. Thanks! Sorry for that silly mistake of mine. I definitely need to practice some focus-attention meditation. Thanks for your great patience.

1 Like