Zeros are replaced in free response if typed at the first position
Hi there,
Sorry for the maybe confusing title of my question. First maybe some background: I'm conducting an online study that infrequently asks participants to report numbers that they have recently seen. The numbers range from 0 to 9 and participants can report up to 4 numbers.
I adapted a script proposed by Sebastiaan in another post. The script is as follows:
{console.log(vars.response)
if (vars.response in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) {
vars.multichar_response += vars.response.toString()
} else if (vars.response === 'backspace') {
vars.multichar_response = vars.multichar_response.toString().slice(0, vars.multichar_response.toString().length - 1)
} else if (vars.response !== 'enter') {
vars.multichar_response += vars.response
}
}
Now it has come to my attention, that when participants type a "0" first and then some other numbers, the 0 is overwritten by the subsequent number. However, if participants report a 0 alone or in between other numbers, everything works just fine.
Does anyone have a suggestion how I can adapt the script so that also zeros remain when typed in first and are then followed by other numbers?
Thanks a lot already in advance!
Best,
Christian

Comments
Hi Christian,
this is a typical problem that comes with your variable type.
Your variable is set as a number. Numbers do not start with a zero. If you manage to change the variable type to character/string (which means letters or names) this should not happen.
Another option would be to make it a list of numbers.
var = 012 --> saves as var = 12
var = '012' --> saves as var = '012' keep in mind that you are not working with a number any more. So, '012' - 10 is not possible. It treats '012' like a word. You cannot substract numbers from words ;)
var = [0,1,2] --> is a list of three numbers. --> I will need to learn JavaScript. In python you could use 'append', so the number of items in the list does not have to be the same.
Hope that helps you out.
Stephan
Hi @ChristianB ,
As @DahmSF pointed out, it has to do with the type of variable. I'm no Javascript expert, but I think that it would be straightforward with pure Javascript variables.
However, I think the problem here might be with how OS handles the variables declared with
vars.. It looks as if for some reason it won't treat it as numerical if it contains only digits and no alphanumerical characters.The problem appears not to be when registering the first response, but when registering the next one. If the second response is a digit, then the leading 0 disappears (it seems that OS then considers that
vars.multichar_responseis a numerical variable of sort). I've adapted your code to allow the response "a", If you first type "0" and then "a", you'll see thatvars.multichar_responseactually contains "0a" (string variable)! However, if you typed "0", "1" and then "a",vars.multichar_responsewould first be equal to "0", "1" and then "1a".I played around a bit and modified your code to:
console.log("REPSONSE:"+vars.response) if (vars.response in [0,1, 2, 3, 4, 5, 6, 7, 8, 9, 'a'] && vars.multichar_response!="") { vars.multichar_response += vars.response.toString() } else if (vars.response==0 && vars.multichar_response=="") { console.log("Leading zero detected") vars.leading_zero="y" vars.multichar_response="0" } else if (vars.response === 'backspace') { vars.multichar_response = vars.multichar_response.toString().slice(0,vars.multichar_response.toString().length - 1) } else if (vars.response !== 'enter') { vars.multichar_response += vars.response } if (vars.leading_zero=="y" && vars.multichar_response.toString().length>0){ var responsestring = vars.multichar_response.toString().padStart(vars.multichar_response.toString().length+1,'0') } else { var responsestring=vars.multichar_response } vars.multichar2=responsestring console.log("RESPONSE STRING LENGTH: "+responsestring.length) console.log("RESPONSE STRING: "+responsestring) console.log("MULTICHAR_RESPONSE LENGTH: "+vars.multichar_response.toString().length) console.log("MULTICHAR_RESPONSE: "+vars.multichar_response) console.log("MULTICHAR2 "+vars.multichar2)(having set
vars.multichar_responseto""before the loop begins)I'm setting up an actual Javascript variable (
var responsestring) as opposed to declaring it using thevars.method. When doing so, I can use code to detect when a leading zero is typed and if it is, use the.padStartfunction applied to the.toStringfunction of thevars.multichar_responsevariable. You'll see thatresponsestringdoes work and contain all digit responses including the leading zero.The minute I try to pass it onto a newly created
vars.multichar2variable, the leading zero disappears (unless the content ofvars.multichar2contains at least one alphanumerical character somewhere).You can try out the example I worked on. Check out the console to monitor everything going on.
In summary, I couldn't work out how to keep a leading zero as part of a variables declared with the
vars.method. I think the problem is not a Javascript one but has to do with the way OS interfaces with Javascript to handle variables.Perhaps the developers can shed some more light on this (I'm tagging @sebastiaan in case he or someone else on the team have suggestions to solve this issue).
The only thing I can think for now is to use a variable to detect whether there is a leading zero (as I've done in the code above) and then save that information in the data log, so that you can afterwards know that participants actually pressed zero first and add it in the data set afterwards (or is your program has to do different things if participants start with zero, you can use that variable to that effect). If you need to display the participants' responses on the screen as they type, you could use two distinct feedback objects, condition their presentation on the value of whatever variable you use to detect if there is a leading zero, and on the feedback object to be show when that is the case, display "0[multichar_response]" instead of "[multichar_response]" (as I've done in the example above).
So, unsolved, but I hope that the above helps you to find a workaround solution...
Perhaps other users with greater experience of Javascript and OS will have better solutions!
Fab.
That behaviour is intentional: https://osdoc.cogsci.nl/3.3/manual/variables/#smart-variable-typing-and-some-pitfalls
Hi @eduard,
Thanks for this! That makes sense 👍️
Fabrice.
Hi @ChristianB,
As an afterthought... One thing you could do is to accept the "0" (zero) as input but save an "O" (o letter) into
vars.multichar_response. Perhaps it works out well in your design. Depending on what font you use on the screen, subjects would not see the difference between "O" and "0"...Try editing the code in my example above and replace "0" by "O"...
Best,
Fabrice.
Sorry for my late response.
Thank you all for your Input. Quite an obvious oversight on my side. However, with your input, I was able to fix the issue! :)
Best,
Christian
Hi @ChristianB and @Fab ,
I see it's already been resolved 👍️
Another way would've been to initialize the variable with something that actually isn't shown, but that does prevent the string from being converted to a number. For example, a
(non-breaking space):— Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!