| Reference | Downloads | Github

"int()" variable in Javascript


I’m having difficulties with translating this code (which works fine in Python) into a JS code.
I wonder, what’s the equivalent to “int()” here? I already tried Number(), but that didn’t seem to work.

if int(Blocknumber) == 10 or int(Blocknumber) == 20: 
   continueRoutine = False
if int(Blocknumber) == 30 or int(Blocknumber) == 40: 
   continueRoutine = False

Thank you for your help!

Hi Rebecca,

As far as I know, there is no integer/decimal issue in the JavaScript and all of the numbers have the type of number. So, if the Blocknumber variable is already a number, the code below may work. If not, you need to convert it to a number first using Number (Blocknumber); and then:

if (Blocknumber === 10 || Blocknumber === 20) {
    continueRoutine = false;
} else if (Blocknumber === 30 || Blocknumber === 40) {
    continueRoutine = false;

I am not completely sure if this code is working and @dvbridges can help you more.

1 Like

Thank you @Omidreeza!
I tried this, but unfortunately it didn’t work. I’m using the int() variable several times in my experiment, but it never works and I tried several options.

Then you need to provide more information for us to see what is the problem.

For example, can you run this experiment on your computer (not online)?

What is the error message in the online version?

What is this blockNumber variable? where is it comming from? What is its expected value (float or string) and why do you want to change it to integer?

The experiment works on my computer, using the Python code mentioned above.
There is no error message when I type it the way you did or if I replace int() with Number(), but it doesn’t end the routine the way it should.
“Blocknumber” is the name of a column within an excel sheet, which shows the number of the block the participant is going through. If the participant is at a certain block (10/20/30/40), then I want it to stop continuing, since this is the first practice trial in which participants should only practice and not already continue with the main part of the experiment.

Oh, I see. That is not surprising then. I think I cannot help you with this question because I am not sure how JavaScript interact with csv and xlsx files. I thought Blocknumber is a simple variable in your codes, but intuitively, it should not make any differences.

So, to find an answer, I recommend to post your problem here again in one comment, include Javascript codes, an screenshot of your xlsx columns, and an screenshot of your experiment routine, and finally, mention of of the creators (preferably dvbridges).

Sorry I could not help more. Good luck with your experiment.

Rebecca, you should be able to do the conversion using something like Number() or possibly parseInt()

So if those aren’t working you’ll need to give us more information than “doesn’t seem to work” :wink: Script fails to load? Raises error message? Always evalutates as false? Something more weird? Your report is currently equivalent to “My phone doesn’t work. Does anyone know why?”

Ultimately, you might need to use your browser debugging tools to insert a breakpoint in your JavaScript at the point where your if statement occurs and use that to find out what exactly the values are of the variable when you try to compare them.

Generally speaking, numeric variables in javascript are floating point; there is no integer data type. Exact comparison with floating point can be tricky, and if you don’t know what you are doing, you can find yourself in trouble, but comparison of exact integer values is usually unproblematic.

In python, the function int converts a floating point number to an integer data type, and truncates toward zero, e.g. int(3.24) => 3 and int(-5.75) => -5.

to get a similar effect in javascript, your have a variety of options, including (careful) use of Math.floor and Math.ceil, or Math.trunc, or parseInt. For a comparison of what all these do, see this:

So the experiment worked now. I inserted parseInt() for each int() and I had to transfer the code to “Each frame” instead of “Beginning of Experiment”.
Thank you all for your help!