psychopy.org | Reference | Downloads | Github

Multiple round brackets in a Javascript if-statement

Dear Experts,

My apologies for the basic question- I am new to Javascript and have a question that would help me troubleshoot issues in my code (and future codes). I am translating some Python code to Javascript and am confused about why the automatic translator in Builder adds a round bracket for every condition that is embedded in the if statement. Is that the correct thing to do? I did not find this in any online references; the ones I found just use one round bracket.

Please see below for an example- I am referring to the round brackets that follow the first “if” statement.

Code in Python:

msg= ''
if (Square_Prac_Resp_1.keys is not None) and (Square_Prac_Resp_2.keys is not None) and (Square_Prac_Resp_3.keys is not None) and (Pic_Prac_Resp_1.keys is not None) and (Pic_Prac_Resp_2.keys is not None) and (Pic_Prac_Resp_3.keys is not None):
    if 'left' in Square_Prac_Resp_1.keys and 'left' in Square_Prac_Resp_2.keys and 'left' in Square_Prac_Resp_3.keys and 'right' in Pic_Prac_Resp_1.keys and 'right' in Pic_Prac_Resp_2.keys and 'right' in Pic_Prac_Resp_3.keys:
        msg = 'Great job! Press the spacebar when you are ready to proceed to the main task.'
    else:
        msg = 'It looks like you made some errors or did not respond in time. Press the spacebar to repeat the practice task.'
else:
        msg = 'It looks like you made some errors or did not respond in time. Press the spacebar to repeat the practice task.'

How I would translate it to Javascript:

var msg = "";
if ((Square_Prac_Resp_1.keys === []) && (Square_Prac_Resp_2.keys === []) && (Square_Prac_Resp_3.keys === []) && (Pic_Prac_Resp_1.keys === []) && (Pic_Prac_Resp_2.keys === []) && (Pic_Prac_Resp_3.keys === [])) {
    msg = "It looks like you made some errors or did not respond in time. Press the spacebar to repeat the practice task.";
} else {
    if ((Square_Prac_Resp_1.keys !== []) && (Square_Prac_Resp_2.keys !== []) && (Square_Prac_Resp_3.keys !== []) && (Pic_Prac_Resp_1.keys !== [])) && (Pic_Prac_Resp_2.keys !== []) && (Pic_Prac_Resp_3.keys !== [])) {
        if ((_pj.in_es6("left", Square_Prac_Resp_1.keys) && _pj.in_es6("left", Square_Prac_Resp_2.keys) && _pj.in_es6("left", Square_Prac_Resp_3.keys) && _pj.in_es6("right", Pic_Prac_Resp_1.keys) && _pj.in_es6("right", Pic_Prac_Resp_2.keys) && _pj.in_es6("right", Pic_Prac_Resp_3.keys)) {
            msg = "Great job! Press the spacebar when you are ready to proceed to the main task.";
        } else {
            msg = "It looks like you made some errors or did not respond in time. Press the spacebar to repeat the practice task.";
        }
    }
}

How it automatically translates:

msg = "";
if (((((((Square_Prac_Resp_1.keys !== null) && (Square_Prac_Resp_2.keys !== null)) && (Square_Prac_Resp_3.keys !== null)) && (Pic_Prac_Resp_1.keys !== null)) && (Pic_Prac_Resp_2.keys !== null)) && (Pic_Prac_Resp_3.keys !== null))) {
    if ((((((_pj.in_es6("left", Square_Prac_Resp_1.keys) && _pj.in_es6("left", Square_Prac_Resp_2.keys)) && _pj.in_es6("left", Square_Prac_Resp_3.keys)) && _pj.in_es6("right", Pic_Prac_Resp_1.keys)) && _pj.in_es6("right", Pic_Prac_Resp_2.keys)) && _pj.in_es6("right", Pic_Prac_Resp_3.keys))) {
        msg = "Great job! Press the spacebar when you are ready to proceed to the main task.";
    } else {
        msg = "It looks like you made some errors or did not respond in time. Press the spacebar to repeat the practice task.";
    }
} else {
    msg = "It looks like you made some errors or did not respond in time. Press the spacebar to repeat the practice task.";
}

@arkadiy, I do not think this is an issue, its just the way the logical operators are parsed, so:

# python

if 1 and 1 and 1 and 1:

becomes

// JavaScript
if (  
     ( ( ( 1 && 1 ) && 1 ) && 1 )
   ) 

As long as the behaviour is correct, then it is nothing to worry about.

1 Like

Perhaps I’m missing something but I’m not sure why you would expect an auto translator to translate a method using and to a method using or.

Got it- thank you!

You are correct! That is my mistake- thank you for catching it.