My pasted Javascript does not work! #1771
Unanswered
python2020-maker
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I pasted my javascript but doesnt work: the task is not ready yet.
My script:
<title>WM Digit Span</title> <script src="jspsych-6.0.4/jspsych.js"></script> <script src="jspsych-6.0.4/plugins/jspsych-html-keyboard-response.js"></script> <script src="jspsych-6.0.4/plugins/jspsych-digit-span-recall.js"></script> <script src="jspsych-6.0.4/plugins/jspsych-survey-text.js"></script> <script src="jspsych-6.0.4/plugins/jspsych-instructions.js"></script> <script src="jspsych-6.0.4/plugins/jspsych-fullscreen.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/jstat@latest/dist/jstat.min.js"></script> <script> /* This is a web-based digit span working memory test. It is modelled after the forward digit span test described in Woods et al (2011) [https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2978794/]. However, users can easily customize this test for their own purposes. Easily customizable variables have been listed below. For further changes to the test, knowledge of JavaScipt may be required. For smooth functioning of the test, make sure all the associated github files within the repository have been downloaded (especially the folder named 'jspsych-6.0.4'). Results from this test will be automatically downloaded into the downloads folder of your desktop. For further details, please refer to the README. *///----- CUSTOMIZABLE VARIABLES -----------------------------------------
nTrials = 14 // number of trials in the test
minSetSize = 3 // starting digit length
stimuli_duration = 1000 // number of miliseconds to display each digit
recall_duration = null // number of miliseconds to allow recall. If null, there is no time limit.
file_name = null // file name for data file. if null, a default name consisting of the participant ID and a unique number is chosen.
local = true // save the data file locally.
// If this test is being run online (e.g., on MTurk), true will cause the file to be downloaded to the participant's computer.
// If this test is on a server, and you wish to save the data file to that server, change this to false.
// If changed to false, ensure that the php file (its in the directory!) and the empty "data" folder has also been appropriately uploaded to the server.
// Incase of problems, feel free to contact me :)
//----------------------------------------------------------------------
possibleNumbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] //possible digits participants can get
var selection = jsPsych.randomization.sampleWithoutReplacement(possibleNumbers, minSetSize) //chooses random digits
var selection_id = -1
// keeps track of participant's scores:
var nError = 0
var highest_span_score = 0
var consec_error_score = 0
// first page. identifies participant for data storage
var p_details = {
type:"survey-text",
questions: [{prompt: "Enter participant number, name, or ID"}],
on_finish:function(){
partN = jsPsych.data.get().last(1).values()[0].partNum
partN = partN.replace(/['"]+/g,'')
// console.log(partN[0])
}
}
// instruction page
var instructions = {
type: 'instructions',
pages: function(){
pageOne = "
INSTRUCTIONS
This is the digit span task.
In this task, you will have to remember a sequence of numbers presented on the screen one after the other.
At the end of each trial, enter all the numbers into the onscreen number-pad in their correct order.
Example: if the sequence is '7 4 5 1', enter '7 4 5 1' in this exact order.
You will now be given practice trials to help you understand the task.
Press 'Next' to start the practice trials.
"
return [pageOne]
},
allow_backward: false,
button_label_next: "Next",
show_clickable_nav: true
}
var instructions_node = {
type: 'instructions',
pages: function(){
pageOne = "
You have completed the practice trials.
If you have further questions about the task, ask the researcher now.
If you are clear about the task, click 'Next' to proceed to the main trials.
"
return [pageOne]
},
allow_backward: false,
button_label_next: "Next",
show_clickable_nav: true,
on_finish: function(){
minSetSize = 3
selection = jsPsych.randomization.sampleWithoutReplacement(possibleNumbers, minSetSize)
}
}
var test_stimuli = {
type: 'html-keyboard-response',
stimulus: function(){
selection_id+=1
return '
'+selection[selection_id]+'
'
},
choices: jsPsych.NO_KEYS,
trial_duration: stimuli_duration,
on_finish: function(){
if (selection_id + 1 >=selection.length){
jsPsych.endCurrentTimeline()
}
}
}
var recall = {
type: 'digit-span-recall',
correct_order: function(){
return selection
},
trial_duration: recall_duration,
on_finish: function(){
acc = jsPsych.data.get().last(1).values()[0].accuracy;
if (acc==1){
if (highest_span_score < minSetSize){
highest_span_score = minSetSize
}
minSetSize+=1
nError = 0
} else if (nError < 1) { // checks for number of consecutive errors
nError += 1
} else {
if (consec_error_score < minSetSize){
consec_error_score = minSetSize
}
minSetSize = Math.max( 3, minSetSize-1)
}
if (minSetSize<=8){ // bottom code prevents immediate repition of digits
selection = jsPsych.randomization.sampleWithoutReplacement(possibleNumbers, minSetSize)
} else {
selection = jsPsych.randomization.sampleWithoutReplacement(possibleNumbers, 8)
var extra = minSetSize-8
var id = possibleNumbers.indexOf(selection[7])
possibleNumbers.splice(id, 1);
var extraNumbers= jsPsych.randomization.sampleWithoutReplacement(possibleNumbers, extra)
selection = selection.concat(extraNumbers)
possibleNumbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
}
selection_id = -1
}
}
var feedback = {
type: 'html-keyboard-response',
stimulus: function(){
var text = ""
var accuracy = jsPsych.data.get().last(1).values()[0].accuracy
if (accuracy==1){
text += '
Correct
'
} else {
text += '
Incorrect
'
}
//text += '
New trial starting now.
'
return text
},
choices: jsPsych.NO_KEYS,
trial_duration: 1000
}
var conclusion = {
type: 'html-keyboard-response',
stimulus: function(){
return '
You have completed the task.
Thank you for your participation in this task.
Maximum number of digits recalled correctly was ' + highest_span_score +'.
Maximum number of digits reached before making two consecutive errors was ' +consec_error_score+'.
Please tell the Research Assistant you have completed the task.
'
},
choices: jsPsych.NO_KEYS
// trial_duration:1000
}
var test_stack = {
timeline: [test_stimuli],
repetitions: 17
}
var test_procedure = {
timeline: [test_stack, recall, feedback],
repetitions: nTrials
}
var demo_procedure = {
timeline: [test_stack, recall, feedback],
repetitions: 3
}
function saveData(filename, filedata){
$.ajax({
type:'post',
cache: false,
url: 'save_data.php', // this is the path to the above PHP script
data: {filename: filename, filedata: filedata}
});
};
var IDsub = Date.now()
var dataLog = {
type: 'html-keyboard-response',
stimulus: " ",
trial_duration: 100,
on_finish: function(data) {
var data = jsPsych.data.get().filter({trial_type: 'digit-span-recall'});
if (file_name == null){
file_name = "WM_digit_span_"+partN+"_"+IDsub.toString()+".csv"}
else{
file_name += ".csv"
}
if (local){
data.localSave('csv', file_name )
} else {
saveData(file_name, data.csv());
}
}
}
timeline = [p_details]
timeline.push({
type: 'fullscreen',
fullscreen_mode: true
});
timeline = timeline.concat([instructions, demo_procedure, instructions_node, test_procedure])
timeline.push({
type: 'fullscreen',
fullscreen_mode: false
});
timeline.push(dataLog)
timeline.push(conclusion)
jsPsych.init({
timeline: timeline,
on_finish: function() {
jsPsych.data.displayData(); // comment out if you do not want to display results at the end of task
}
});
</script>
Beta Was this translation helpful? Give feedback.
All reactions