Showing Sum of Money Either Gained or Lost #858
-
Hello Everyone! For my code, I will be having people choose numbers on a number line and the numbers are associated with some monetary value. So far in my code, I have made it such that when someone enters a number, they will receive a response letting them know how much money they gained or lost. I was wondering if there was a way to have a sum/ net total of all the money the person either gained or lost at the end of the task? The person will go through ten trials of choosing a number, and after the ten trials, I would like to show them how much they either gained or lost. My code is below. Thank you in advance! Any help is greatly appreciated :)
var survey = {
type: "survey-text",
questions: [{ prompt: "Enter a number:", columns: 3, name: "Number" }],
data: { task: "guess-number" }
};
var feedback = {
type: "html-keyboard-response",
stimulus: function() {
var guess = JSON.parse(
jsPsych.data
.get()
.filter({ task: "guess-number" })
.last(1)
.values()[0].responses
).Number;
if (0 <= guess && guess <= 49) {
return "<p>Gain one cent (+ 1¢)</p>";
} else if (71 <= guess && guess <= 100) {
return "<p>Gain one cent (+ 1¢)</p>";
} else if (guess == 50 || guess == 70) {
return "<p>Lose ten cents (- 10¢)</p>";
} else if (guess == 55 || guess == 65) {
return "<p>Gain no money (+ 0¢)</p>";
} else if (guess == 60) {
return "<p>Gain one dollar (+ 1$)</p><p>Congratulations! You found the winning number!</p><p>(Please enter '60' for the remaining trials)</p>";
} else {
//guess <= 0 || guess >= 100
return "<p>Invalid number.</p><p>Remember, please enter in a number between 0 and 100 for the remaining trials!</p>";
}
},
choices: jsPsych.NO_KEYS,
trial_duration: 3000
};
var actual_procedure = {
timeline: [fixation, numberline, survey, feedback],
repetitions: 10
};
timeline.push(actual_procedure);
jsPsych.init({
timeline: timeline
}); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You can have a variable outside the experiment that keeps track of the totals. var survey = {
type: "survey-text",
questions: [{ prompt: "Enter a number:", columns: 3, name: "Number" }],
data: { task: "guess-number" }
};
var TOTAL = 0;
var feedback = {
type: "html-keyboard-response",
stimulus: function() {
var guess = JSON.parse(
jsPsych.data
.get()
.filter({ task: "guess-number" })
.last(1)
.values()[0].responses
).Number;
if (0 <= guess && guess <= 49) {
TOTAL += 1;
return "<p>Gain one cent (+ 1¢)</p>";
} else if (71 <= guess && guess <= 100) {
TOTAL += 1;
return "<p>Gain one cent (+ 1¢)</p>";
} else if (guess == 50 || guess == 70) {
TOTAL -= 10;
return "<p>Lose ten cents (- 10¢)</p>";
} else if (guess == 55 || guess == 65) {
return "<p>Gain no money (+ 0¢)</p>";
} else if (guess == 60) {
TOTAL += 100;
return "<p>Gain one dollar (+ 1$)</p><p>Congratulations! You found the winning number!</p><p>(Please enter '60' for the remaining trials)</p>";
} else {
//guess <= 0 || guess >= 100
return "<p>Invalid number.</p><p>Remember, please enter in a number between 0 and 100 for the remaining trials!</p>";
}
},
choices: jsPsych.NO_KEYS,
trial_duration: 3000
};
var actual_procedure = {
timeline: [fixation, numberline, survey, feedback],
repetitions: 10
};
var display_total = {
type: "html-keyboard-response",
stimulus: function() {
return (TOTAL / 100).toString() + " is what you have gained.";
}
};
timeline.push(actual_procedure, display_total);
jsPsych.init({
timeline: timeline
}); |
Beta Was this translation helpful? Give feedback.
You can have a variable outside the experiment that keeps track of the totals.