these appear correctly according to the below line of code when run locally on PsychoPy. Account_value is determined before in the code and it’s correct. The problem is with reading the strings from Excel.
msg = "You managed to win $" + String(win) + "\nTotal amount of money: $" + String(account_value);
in Pavlovia, I receive an error:
“You managed to win $NaN. Total amount of money: $NaN”
I tried to format numbers, delete the online experiment and re-run it.
One thing that I found weird is that sometimes the values are returned correctly, but I’m not sure I’m changing anything to make it so.
Is this a known bug?
I did find some threads on this, but on blank spaces or other, not this particular example where the values are actually written in Excel.
As tandy suggested: no String() or string() before the variables - the experiment no longer loads in pavlovia, it gets stuck. Looking into the developer tools, I observed that I received an error for one of the variables (none of the rest):
“Unexpected identifier at line 2077” → another “nono” variable is at that line.
After I tried to write string() instead of String(), the same error appears.
Regardless on what I do now (I deleted and re-created the project 3-4 times). I still receive that error.
The variable was never named “win”, it’s actually named otherwise.
I did not think the name would have an influence, the “win” name was provided as an example.
So the problem was that I had forgotten a “+” sign into the sentence → after I put it, I got rid of the “Unexpected identifier at line 2077” error
Yes, I copied the Excel into another to make it a dummy - I will want to run this and I don’t want anyone to figure out anything to tamper with the results of the experiment.
I now left everything without String() - it did not recognize string() with a small capital letter.
However, I intermittently receive either $NaN or the numbers - much more often the $NaN.
I also checked several times the numbers in Excel, they’re formatted as numbers, no decimal places, copied and pasted them as values there to make sure that they are numbers.
I tried using str() and it returned the error:
" * ReferenceError: str is not defined"
I thought that str(0 was for Python code - locally everything works fine.
On Pavlovia and in javascript, I can’t get it to work.
The first iteration almost always works, then as the code advances (loops) through the Excel rows that it’s referenced, I still receive the $NaN / NaN message.
I tried using String() and .toString() and also left it with no string - I cannot get rid of the NaN message.
Is this an issue with the PSychoPy code version?
I am using 2021.2.3 - how do I change it fi this is the error?
Or is it related to the Excel file (I cannot see how exactly…but?)
Thanks in advance, I’m quite stressed to get this to work.
I
I don’t know where exactly to put print(‘loss’,loss). I placed it right before the if structure and the browser stopped as if wanting to print on a printer.
Regardless, I think I found the source of the problem, but I still don’t know how to fix it.
I believe the problem comes from the fact that the online JS/Pavlovia code cannot read the variables from the Excel file - not all of them at least. The first iteration is always read. But after the first one, I always receive NaN.
For example, when I comment the line loss=Math.round(loss,0), I receive “undefined” for loss.
But that’s weird because loss is defined in the JS code using “var loss” and in the Excel file.
Looking into other questions on the forum, I saw that this may stem from the trialList component of the js code:
This is a problem. If you have a variable defined in a spreadsheet, don’t also define it in a code component.
If you are coding in JSPsych from scratch then I’m afriad I have no idea what might or might not work. All of my advice is based on using Builder to create the JS code, which is why I give Python code (like print) not JS code (like console.log) in my posts.
I wrote the code in python.
I did not define the variable using the var declaration (e.g var loss).
They are defined automatically from Python to JS using PsychoPy (Auto-JS).
I had to modify one thing only in JS and that’s related to randomly choosing a variable.
Instead of importing * as random in JS (which resulted in an error for unexpected token “*”), I had to use:
var index = Math.floor(Math.random() * alegeri.length);
alegere=alegeri[index]
I’ve since been trying to not get NaN.
If I remove the “round(variable,0)” code lines from Python, do the Auto-JS code translation and introduce the randomization as above, the variables are still not read correctly in JS.
The problem is that Pavlovia is not reading my Excel.
Any idea why?
The excel is in a loop that should be read 10 times, each row of that Excel being like an iteration:
Where should I look to make sure that all variables defined in the Excel are correctly read in Pavlovia?
I defined all variables as you mentioned i.e. loss2=round(loss,0), nono2=round(nono,0) etc.
However, the problem is still there.
I still receive the NaN message → because the initial values (i.e. loss, nono etc.) are not correctly (or at all?) read from the Excel file.
This is the code section where the Excel file is read. Is it wrong? Am I missing something? All cells are defined as numbers in Excel (tried defining them as General or Accounting , but still nothing).
Should trialList: “excel1.xlsx” be written otherwise?
I saw in a post from you that you defined it differently:
And your code was defined as:
var loading_loop;
var currentLoop;
function loading_loopLoopBegin(loading_loopLoopScheduler) {
// set up handler to look after randomisation of conditions etc
loading_loop = new TrialHandler({
psychoJS: psychoJS,
nReps: 3, method: TrialHandler.Method.RANDOM,
extraInfo: expInfo, originPath: undefined, trialList: TrialHandler.importConditions(psychoJS.serverManager, ‘stimuli.xlsx’, ‘0:10’),
seed: undefined, name: ‘loading_loop’
});
psychoJS.experiment.addLoop(loading_loop); // add the loop to the experiment
currentLoop = loading_loop; // we’re now the current loop
I tried doing that, but I received errors compiling.