Home > Enterprise >  Trying to create a cumulative score and present a total score for an experiment using Javascript
Trying to create a cumulative score and present a total score for an experiment using Javascript

Time:09-30

I'm building an experiment that gives people points depending on how fast they respond to a target. The faster they respond, the more points they can earn. Points within this 'blue' condition start at 8000, and the faster they respond, the more points they earn. These points are displayed on the screen after each trial.

I was to create a new variable that is an accumulated score, so at the end of the experiment, I can tell them the total amount of points earned.

I have created this Retrieved variable which I want to use at the accumulated total. However, at the end of the experiment, it's just presenting the last score calculated (pointConvertedBlue) and doubling it (e.g, if there are 3 trials in which someone earned: trial 1 = 6000, trial 2 = 6432, trial 3 = 5690, the "total score" becomes 11,380).

I'm a psychology researcher so my coding skills are rough as anything, so I'm really unsure how to fix this problem. If anyone has any pointers I'd be very grateful!

This first section of code is what I need for each trial. This is where I am trying to create an accumulated total within the loop.

if (row.display == _requiredFeedbackDisplay) {
  if (screenIndex == _requiredFeedbackScreen) {
    if (row.TrialNumer >= 1 && row.TrialNumer <= 30) {
      // Retrieve the reaction time from the store
      var lastRT: number = gorilla.retrieve(_RTKey, null, false);
      var roundedRT: number = Math.round(lastRT);
      var pointConvertedBlue: number = 8000 - roundedRT;
      gorilla.store(EMBEDDED_DATA, pointConvertedBlue, true);
      var retrievedValue: number = gorilla.retrieve(EMBEDDED_DATA, 0, true);
      if (retrievedValue != 0) {
        retrievedValue  = pointConvertedBlue;
      }
      gorilla.store(TOTAL_SCORE, retrievedValue, true);
      // We now add the rounded reaction time to the screen
      $(container   _requiredFeedbackZoneSelector).html(
        "<h1>"  
          '<p style="color: white;">'  
          "You earned "  
          pointConvertedBlue  
          " points"  
          "</p> "  
          "</h1>"
      );
      // Using gorilla.refreshLayout here will make sure that everything is displayed nicely on the screen
      gorilla.refreshLayout();
      gorilla.metric({
        trial_number: "pointConvertedBlue",
        response: pointConvertedBlue,
      });
    }
  }
}

This next section of code is for when I have finished all trials, on the break screen, and I want to present a total score.

if (row.display == Block_Break) {
  if (screenIndex == Block_screen) {
    var totalScore: number = gorilla.retrieve(TOTAL_SCORE, 0, true);
    $(container   _requiredFeedbackZoneSelector3).html(
      "<h1>"  
        '<p style="color: white;">'  
        "So far, you have earned a total of      "  
        totalScore  
        " points"  
        "</p> "  
        "</h1>"
    );
    gorilla.refreshLayout();
  }
}

CodePudding user response:

The doubling of the value is a result of the order of store and load operations here:

gorilla.store(EMBEDDED_DATA, pointConvertedBlue, true);
var retrievedValue: number = gorilla.retrieve(EMBEDDED_DATA, 0, true);
if (retrievedValue != 0) {
    retrievedValue  = pointConvertedBlue;
}
gorilla.store(TOTAL_SCORE, retrievedValue, true);

You are storing the last score in EMBEDDED_DATA from pointConvertedBlue, than loading the value of EMBEDDED_DATA into retrievedValue. So in the end retrievedValue is the same as pointConvertedBlue. My guess is, that you accidentially wrote EMBEDDED_DATA instead of TOTAL_SCORE when retrieving retrievedValue. You could try the following:

gorilla.store(EMBEDDED_DATA, pointConvertedBlue, true);
var retrievedValue: number = gorilla.retrieve(TOTAL_SCORE, 0, true);
if (retrievedValue != 0) {
    retrievedValue  = pointConvertedBlue;
}
gorilla.store(TOTAL_SCORE, retrievedValue, true);

CodePudding user response:

I've worked with the Gorilla API very briefly, but it looks like the issue here may be a semantic error with this part of your code:

var pointConvertedBlue: number = 8000 - roundedRT;
gorilla.store(EMBEDDED_DATA, pointConvertedBlue, true);
var retrievedValue: number = gorilla.retrieve(EMBEDDED_DATA, 0, true);
if (retrievedValue != 0) {
   retrievedValue  = pointConvertedBlue;
}

gorilla.store(TOTAL_SCORE, retrievedValue, true);

It looks like you store the converted value, retrieve it, and add it to itself. If you don't have to store values before performing operations on them (like you did with pointConvertedBlue), you could switch the lines like so:

var pointConvertedBlue: number = 8000 - roundedRT;
gorilla.store(EMBEDDED_DATA, pointConvertedBlue, true);
var totalScore: number = gorilla.retrieve(TOTAL_SCORE, 0, true);
// don't really need the conditional because gorilla.retrieve returns '0' by default according to above param.
var newTotal: number = totalScore   pointConvertedBlue
gorilla.store(TOTAL_SCORE, newTotal, true);


  • Related