# Moving window code component not working

**URL:** https://discourse.psychopy.org/t/moving-window-code-component-not-working/13210
**Category:** Online experiments
**Created:** [May 11, 2020, 3:11am UTC](https://discourse.psychopy.org/t/moving-window-code-component-not-working/13210 "2020-05-11T03:11:09Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![matheusba](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/matheusba/32/8335_2.png) [@matheusba](https://discourse.psychopy.org/u/matheusba)
#### Post date: [May 11, 2020, 3:11am UTC](https://discourse.psychopy.org/t/moving-window-code-component-not-working/13210/1 "2020-05-11T03:11:10Z")

</div>

URL of experiment: [Sign in · GitLab](https://gitlab.pavlovia.org/matheus/mwindow_code)

Description of the problem: Hello everyone!  
I have built an experiment in the Builder and attached a code component to some routines. The experiment is a moving window self-paced reading task.  
Although it runs smoothly when I run it on PsychoPy, I’ve been facing some issues while trying to run it online.  
Since I don’t know much (anything at all, actually) about programming languages, I talked to a developer who translated the codes of the code component so I could start from somewhere. We’ve been trying to get it to run online for the past days, and yet, nothing works.

I honestly have no clue what to do next. If anyone can shed some light, I’d be very thankful for anything, really!

There are basically 2 code in the components (that repeat on some other routines, changing the text component they associate with as well as the column in the xlsx file).

The _begin routine_:  
**Python**

> sentenceTREINOList = sentenceTREINO.split(‘/’)  
> #this breaks your sentence’s single string of characters into a list of individual  
> #words, e.g. ‘The quick brown fox.’ becomes [‘The’, ‘quick’, ‘brown’, ‘fox.’]
> 
> #keep track of which word we are up to:  
> wordNumber = -1 # -1 as we haven’t started yet
> 
> #now define a function which we can use here and later on to replace letters with ‘x’:
> 
> def replaceWithX(textList, currentWordNumber):
> 
> ```
> xSentenceTREINO = ''
> for index, word in enumerate(textList): # cycle through the words and their index numbers
> if index != currentWordNumber:
> xSentenceTREINO = xSentenceTREINO + '_' * len(word) + ' ' # add a string of x characters
> else:
> xSentenceTREINO = xSentenceTREINO + word + ' ' # except for the current word
> 
> return xSentenceTREINO # yields the manipulated sentence
> 
> ```

**JavaScript** :

> var setenceTREINOlist = sentenceTREINO.split(‘/’);
> 
> var wordNumber = -1;
> 
> function replaceWithX(textList,current){  
> var xSetenceTREINO = ‘’;  
> var index = 0;  
> for(word in Object.values(textList)){
> 
> ```
> if(index != currentWordNumber){
> xSetenceTREINO = '_' * word.length + ' ';   
> }else{
> xSentenceTREINO = xSentenceTREINO + word + ' ';
> }
> index = index+1;
> }
> return xSentenceTREINO;
> 
> ```
> 
> }

And the _each frame_:  
**Python**

> #now at the very beginning of the trial, we need to run this function for the  
> #first time. As the current word number is -1, it should make all words ‘x’.  
> #Use the actual name of your Builder text component here:
> 
> text\_10.text = replaceWithX(sentenceTREINOList, wordNumber)
> 
> #In the Builder interface, specify a “constant” value for the text content, e.g.  
> #‘test’, so it doesn’t conflict with our code.
> 
> keypresses = event.getKeys() # returns a list of keypresses
> 
> if len(keypresses) \> 0: # at least one key was pushed
> 
> ```
> if 'space' in keypresses:
> 
> thisResponseTime = t # the current time in the trial
> wordNumber = wordNumber + 1
> if wordNumber < len(sentenceTREINOList):
> 
> if wordNumber == 0: # need to initialise a variable:
> timeOfLastResponse = 0
> 
> # save the inter response interval for this keypress,
> # in variables called IRI_0, IRI_1, etc:
> thisExp.addData('IRI_TREINO_' + str(wordNumber), thisResponseTime - timeOfLastResponse)
> timeOfLastResponse = thisResponseTime
> 
> # update the text by masking all but the current word
> text_10.text = replaceWithX(sentenceTREINOList, wordNumber)
> else:
> continueRoutine = False # time to go on to the next trial
> 
> elif 'escape' in keypresses:
> 
> core.quit() # I think you'll need to handle quitting manually now.
> 
> ```

**JavaScript** :

> text\_10.text = replaceWithX(sentenceTREINOList, wordNumber)
> 
> var keypresses= event.getKeys();
> 
> if (keypresses.lenght \> 0){
> 
> ```
> if(keypresses.include('space')){
> thisResponseTime = t ;
> wordNumber = wordNumber + 1;
> if (wordNumber < sentenceTREINOList.lenght){
> if (wordNumber == 0){ 
> timeOfLastResponse = 0;
> }
> thisExp.addData('IRI_TREINO_' + str(wordNumber), thisResponseTime - timeOfLastResponse)
> timeOfLastResponse = thisResponseTime;
> text_10.text= replaceWithX(sentenceTREINOList, wordNumber)
> }
> else{
> continueRoutine = False 
> }
> }
> else if (keypresses.include('escape')){
> core.quit();
> }
> 
> ```
> 
> }

---

<div class="post-metadata">

### Author: ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)
#### Post date: [May 11, 2020, 5:43am UTC](https://discourse.psychopy.org/t/moving-window-code-component-not-working/13210/2 "2020-05-11T05:43:33Z")

</div>

I have found that setting a Builder component to update every frame doesn’t work. You should set it to constant and then update it in the code.

Have a look at my crib sheet [https://docs.google.com/document/d/13jp0QAqQeFlYSjeZS0fDInvgaDzBXjGQNe4VNKbbNHQ/edit?usp=sharing](https://docs.google.com/document/d/13jp0QAqQeFlYSjeZS0fDInvgaDzBXjGQNe4VNKbbNHQ/edit?usp=sharing)

Best wishes,

Wakefield

---

<div class="post-metadata">

### Author: ![matheusba](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/matheusba/32/8335_2.png) [@matheusba](https://discourse.psychopy.org/u/matheusba)
#### Post date: [May 11, 2020, 6:06pm UTC](https://discourse.psychopy.org/t/moving-window-code-component-not-working/13210/3 "2020-05-11T18:06:48Z")

</div>

Thanks for your reply, Wakefield.  
The component in builder is already set as constant.  
The **each frame** above refers to code in the code component of the routine.

---

<div class="post-metadata">

### Author: ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)
#### Post date: [May 11, 2020, 11:05pm UTC](https://discourse.psychopy.org/t/moving-window-code-component-not-working/13210/4 "2020-05-11T23:05:55Z")

</div>

Did you get someone to manually translate the code to JavaScript? If so, then I’m probably not going to be able to help, since I’m learning errors causes by imperfections in the auto translate.

Also, you haven’t actually said what error you are getting, which is why I gave very generic advice.

---

<div class="post-metadata">

### Author: ![matheusba](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/matheusba/32/8335_2.png) [@matheusba](https://discourse.psychopy.org/u/matheusba)
#### Post date: [May 12, 2020, 6:55pm UTC](https://discourse.psychopy.org/t/moving-window-code-component-not-working/13210/5 "2020-05-12T18:55:51Z")

</div>

Yes, Wakefield. Someone manually translated it for me. Thanks for trying to help anyway!  
So far I am getting the following error:

 ![Captura de Tela 2020-05-12 às 3.54.00 PM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/3/f/3ff26d6238b22dd2abffc73d26ed64d2bab40393.png)

---

<div class="post-metadata">

### Author: ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)
#### Post date: [May 12, 2020, 7:24pm UTC](https://discourse.psychopy.org/t/moving-window-code-component-not-working/13210/6 "2020-05-12T19:24:16Z")

</div>

… is not defined usually means that you need to give the variable a value (or specify it as empty) at the start of the experiment. You can’t just create it for the first time in the routine when you want it.

Best wishes

Wakefield

---

<div class="post-metadata">

### Author: ![paulaluegi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/paulaluegi/32/4615_2.png) [@paulaluegi](https://discourse.psychopy.org/u/paulaluegi)
#### Post date: [October 2, 2020, 9:09am UTC](https://discourse.psychopy.org/t/moving-window-code-component-not-working/13210/7 "2020-10-02T09:09:55Z")

</div>

Hi,

During the last days I’ve been trying to run a Self-Paced Moving Window experiment, very similar to the one @matheusba presented. Instead of using the code he wrote on his post, I used PsychoPy to convert the Python Script into JavaScript (with some corrections to the Python Code @matheusba presented).

I also added the code component @wakecarter suggests in his Crib Sheet (thanks!) at the beginning of the experiment. However, I still get the same error as @matheusba.

When I try to run the experiment online ([here](https://gitlab.pavlovia.org/LabPsicoling_CLUL/movingwindowjavascript2.git)), I get exactly the same error: it does not recognize the function replaceWithX(), and I’ve tried to copy it to the beginning of the experiment, as suggested by @wakecarter, and it also didn’t work.

When I run the experiment on my computer, using PsychoPy (with JS code) it does not work but the problem seems to be different: I believe that the stimulus file is not properly uploaded.

Does anyone have any suggestion that could help us with this problem?

Thank you in advance!

_Paula_

---

<div class="post-metadata">

### Author: ![paulaluegi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/paulaluegi/32/4615_2.png) [@paulaluegi](https://discourse.psychopy.org/u/paulaluegi)
#### Post date: [October 6, 2020, 3:22pm UTC](https://discourse.psychopy.org/t/moving-window-code-component-not-working/13210/8 "2020-10-06T15:22:11Z")

</div>

Hi everyone,

Just to tell to anyone that might be interested in this topic that I had some help and the solution is as follows:

1. in the component code, in Begin Routine tab add:

```auto
sentenceTREINOList = sentenceTREINO.split("/");
wordNumber = (-1);

```

1. in Each Frame tab add:

```auto
var _pj;
function replaceWithX(textList, currentWordNumber) {
    var index, result, word;
    result = "";
    index = 0;
    while ((index < textList.length)) {
        word = textList[index];
        if ((index !== currentWordNumber)) {
            result = ((result + ("_".repeat(word.length))) + " ");
        } else {
            result = ((result + word) + " ");
        }
        index = (index + 1);
    }
    return result;
}
function _pj_snippets(container) {
    function in_es6(left, right) {
        if (((right instanceof Array) || ((typeof right) === "string"))) {
            return (right.indexOf(left) > (- 1));
        } else {
            if (((right instanceof Map) || (right instanceof Set) || (right instanceof WeakMap) || (right instanceof WeakSet))) {
                return right.has(left);
            } else {
                return (left in right);
            }
        }
    }
    container["in_es6"] = in_es6;
    return container;
}
_pj = {};
_pj_snippets(_pj);
text_10.text = replaceWithX(sentenceTREINOList, wordNumber);
keypresses = event.getKeys();
if ((keypresses.length > 0)) {
    if (_pj.in_es6("space", keypresses)) {
        thisResponseTime = t;
        wordNumber = (wordNumber + 1);
        if ((wordNumber < sentenceTREINOList.length)) {
            if ((wordNumber === 0)) {
                timeOfLastResponse = 0;
            }
            thisExp.addData(("IRI_TREINO_" + wordNumber.toString()), (thisResponseTime - timeOfLastResponse));
            timeOfLastResponse = thisResponseTime;
            text_10.text = replaceWithX(sentenceTREINOList, wordNumber);
        } else {
            continueRoutine = false;
        }
    } else {
        if (_pj.in_es6("escape", keypresses)) {
            core.quit();
        }
    }
}

```

As mentioned on the previous post, I also added the code component @wakecarter suggests in his Crib Sheet at the beginning of the experiment.

Now the experiment runs perfectly in Pavlovia.

Thank you very much.

_Paula_
