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 31 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 31

    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.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games