Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Sort the concatenated response and compare it to the answer

edited March 2022 in OpenSesame

Hello,

I'm not a JAVA script user, so I really need your help to code this out.

My task is to let people guess three numbers out of four (1, 2, 3, & 4).

However, the answer can be one of the permutations of these numbers, so I need to sort their response in numerical order and compare it to the answer to give them feedback (i.e., if they typed in 321, I should sort this into 123 and comparing it to the answer).

This is also related to my previous question on how to make receive three responses at a time. Thank you! https://forum.cogsci.nl/discussion/7816/can-i-show-keyboard-response-to-the-participant#latest

Comments

  • Hi @Soan,

    If I understand you well, you want to be able to sort the responses produced by the participant.

    Starting from the task example in another thread (https://forum.cogsci.nl/discussion/7816/can-i-show-keyboard-response-to-the-participant#latest), I modified slightly the code.

    First, I noticed that in the example I posted earlier, while it worked well when participants pressed letters (or at least started with a letter), a problem occurred when they pressed only digits. THis is because Open Sesame then automatically treats the variable as a numerical one, so that when we try to concatenate the responses (e.g., "1", "2", "3") we get "6" (1+2+3) instead of "123". I fixed that with a small modification of the concatenate_responses code:

    if (vars.response == 'backspace') {
    if (vars.respcount>0) {
    vars.concatenated=vars.concatenated.slice(0, -1)
    vars.respcount-=1
    }
    } else {
    vars.concatenated+=vars.response.toString()
    vars.respcount+=1
    }
    
    // output to console for checking
    console.log ("Last response: "+vars.response)
    console.log ("All registered responses: "+vars.concatenated)
    console.log ("Valid response count: " +vars.respcount)
    

    The change consists in adding vars.response.toString() instead of vars.response to vars.concatenated.

    Now on to the issue of sorting the responses in ascending order.

    There are certainly several ways to do that but one simple way is to transform the vars.concatenated variable into an array (that is, a variable with distinct elements). and to make use of the .sort function available in Javascript.

    I inserted the code after the loop where the three responses have been collected:

    Here is the code:

    var myArray = new String()
    myArray =vars.concatenated.toString()
    myArray=myArray.split('')
    console.log(myArray)
    myArray=myArray.sort()
    console.log(myArray)
    
    // saves each individual response in a separate variable (incouding these in the logger if you want them regostered in the output)
    // !! This example does not include a logger; you'd have to add it to your task where appropriate
    vars.response1=myArray[0]
    vars.response2=myArray[1]
    vars.response3=myArray[2]
    
    // assenmbles the sorted array into a string variable
    // Add sorted_concatenaed to the logger if you want it saved
    vars.sorted_concatenated=myArray.toString()
    console.log ("Sorted response sequence: "+vars.sorted_concatenated)
    

    The first part is where I declare the array as a string first and then put the content of the vars.concatenated into it (taking the string value of it). I then split the array (so that, for example, "321 becomes an array with three elements: "3", "2", "1"). I then sort the array (myArray=myArray.sort()). At that point, the array contains "1", "2", "3".

    Not knowing whether you want to save the individual elements of that array of a string assembling them, I did both. I first save each element of the sorted array into separated variables (response1, response2, response3) and then create a new variable (sorted.concatenated) where I put the array as a string ("123").

    You can download my modified example here:

    Good luck!

    Fabrice.

    Buy Me A Coffee

  • Thank you!! I've sent you a coffee...! Thank you so much!

  • edited March 2022

    Dear Fabrice. Thank you so much for your code, but the program stops when I try to erase my answer with a backspace... I can solve the problem temporarily by changing the response options to alphabetical ones for now.

  • Hi @Soan,

    Oops, had missed that bit... Indeed, the code struggles with backspace in the specific case where the responses only contain digits because of the way OS handles variables. Whenever a variable contains only digits, it treats it as a numerical variable. hence, in this case, the following line of code generates an error:

    Fortunately, it is easy to fix. We just need to apply the toString() function before we apply the slice function:

      vars.concatenated=vars.concatenated.toString().slice(0, -1)
    

    You should now be able to use the backspace even when only inputting digits.

    I update my example n Dropbox. You can download it using the link in my previous message.

    Best,

    Fabrice.

    PS: Many thanks for the ☕️ yesterday! 😃

    Buy Me A Coffee

  • Thank you sooooo much!!

Sign In or Register to comment.