Skip to content

Commit

Permalink
Unit tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
Poc275 committed Oct 18, 2016
1 parent f37db9e commit 9f260e7
Show file tree
Hide file tree
Showing 14 changed files with 5,088 additions and 67 deletions.
16 changes: 14 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<meta name="description" content="">
<meta name="author" content="">

<title>Bare - Start Bootstrap Template</title>
<title>Dual N-Back Training for memory and intelligence</title>

<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
Expand Down Expand Up @@ -140,7 +140,7 @@ <h4>L</h4>
<!-- button row -->
<div class="row">
<div class="col-xs-2 col-md-offset-5 text-center">
<button>START</button>
<button id="start-button" value="Start">Start</button>
</div>
</div>

Expand All @@ -153,6 +153,18 @@ <h4>L</h4>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>

<!-- NBack js scripts -->
<script src="js/Consonant.js"></script>
<script src="js/SquarePosition.js"></script>
<script src="js/TargetKind.js"></script>
<script src="js/TrialResult.js"></script>
<script src="js/Page.js"></script>
<script src="js/Score.js"></script>
<script src="js/Block.js"></script>
<script src="js/BlockCreator.js"></script>
<script src="js/Trial.js"></script>
<script src="js/App.js"></script>

</body>

</html>
6 changes: 6 additions & 0 deletions js/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
window.addEventListener("load", function(event) {
// Load the main control (Page.js) here
// This mimics the App.OnStartup() function
var page = new Page();
});

30 changes: 18 additions & 12 deletions js/BlockCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@ function BlockCreator() {
// FUNCTIONS
this.createBlock = function(n) {
// get a list of targets (positive trials) that should appear in the trials
var targets = getTargets();
var targets = this.getTargets();

var trials = [];

// the first n trials are completely random
for(var i = 0; i < n; i++) {
var t = getRandomTrial();
var t = this.getRandomTrial();
// too early in the list for this trial to have an earlier matching pair
t.SetSecondTrialInTarget = TargetKind.TooEarly;
t.SetSecondTrialInTarget(TargetKind.TooEarly);
trials.push(t);
}

for(var i = n; i < default_Block_Size + n; i++) {
// get a completely non-matching trial
var trialToAdd = getRandomTrial(trials[i - n]);
var trialToAdd = this.getRandomTrial(trials[i - n]);

var matchingKind;

// is this trial the 2nd in a target ("matching") pair? If so,
// we need to tweak it before we add it so that it matches
if(var matchingKind = targets.find(i - n) != undefined) {
var findResult = targets.find(function(el) {
return el === (i - n);
});
if(findResult != undefined) {
var matchingKind = targets.find(i - n);
var trialAlreadyAdded = trials[i - n];

// set the "matching kind" in the 2nd pair that we're adding
Expand Down Expand Up @@ -57,7 +61,7 @@ function BlockCreator() {

// audio targets
for(var i = 0; i < num_Audio_Targets; i++) {
var iTargetLocation = getRandomTargetLocation(targets);
var iTargetLocation = this.getRandomTargetLocation(targets);
var target = {};
target.Key = iTargetLocation;
target.Value = TargetKind.Audio;
Expand All @@ -66,7 +70,7 @@ function BlockCreator() {

// visual targets
for(var i = 0; i < num_Visual_Targets; i++) {
var iTargetLocation = getRandomTargetLocation(targets);
var iTargetLocation = this.getRandomTargetLocation(targets);
var target = {};
target.Key = iTargetLocation;
target.Value = TargetKind.Visual;
Expand All @@ -75,20 +79,19 @@ function BlockCreator() {

// both targets
for(var i = 0; i < num_Both_Targets; i++) {
var iTargetLocation = getRandomTargetLocation(targets);
var iTargetLocation = this.getRandomTargetLocation(targets);
var target = {};
target.Key = iTargetLocation;
target.Value = TargetKind.Both;
targets.push(target);
}

// sort the targets so they're all in order
// TODO: CHECK IF IT SORTS CORRECTLY BY VALUE
targets.sort(function(a, b) {
if(a.Value > b.Value) {
if(a.Key > b.Key) {
return 1;
}
if(a.Value < b.Value) {
if(a.Key < b.Key) {
return -1;
}
// a must be equal
Expand All @@ -111,7 +114,10 @@ function BlockCreator() {
var k;

// if this value is already used, just stay in the loop
if(targets.find(iLocation) == undefined) {
var findElement = targets.find(function(el) {
return el.Key === iLocation;
});
if(findElement === undefined) {
break;
}
} while(true);
Expand Down
11 changes: 11 additions & 0 deletions js/Consonant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// enum of possible consonants to be played to the user
var Consonant = {
Letter1: 0,
Letter2: 1,
Letter3: 2,
Letter4: 3,
Letter5: 4,
Letter6: 5,
Letter7: 6,
Letter8: 7,
};
44 changes: 33 additions & 11 deletions js/Page.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
function Page() {
var self = this; // for event handlers
var startButton = document.getElementById('start-button');

// our list of trials
this.m_Trials = [];

Expand Down Expand Up @@ -75,6 +78,20 @@ function Page() {
// //Start our timer which calls back to start the application.
// InitialWait.Begin();
//}
startButton.addEventListener('click', function(event) {
// check for start/pause via changing the text value of the button itself
if(event.target.value == "Start") {
event.target.value = "Pause";
event.target.textContent = "Pause";
// original timer = 675 * 10000 (675ms)
// in setTimeout() the delay is in milliseconds
// 'this' in an event handler is the object that fired the event
window.setTimeout(self.Start_Training, 675);
} else if(event.target.value == "Pause") {
event.target.value = "Start";
event.target.textContent = "Start";
}
});

// Reset_Click() {
// StartButton.Content = Page_Resources.Start_Button;
Expand Down Expand Up @@ -136,14 +153,18 @@ function Page() {
// }

// callback for when the timer expires for the stimulus, hide the stimulus
this.hideStimulus() {
//this.hideStimulus() {
//FadeBoxOut.Begin();
}
//}



// FUNCTIONS
this.Start_Training = function() {
alert("Started Training!");

var blockCreator = new BlockCreator();

_playingGame = true;

//Set the stimulus fade-out timer
Expand All @@ -159,14 +180,15 @@ function Page() {
//_score.HandleScores += handleScores;

// we're starting, get the list of trials and clear the extra stuff
_n = _starting_N;
_n = self._starting_N;
_blockNum = 0;
_trialNum = 0;
m_Trials = BlockCreator.createBlock(_n);
//m_Trials = BlockCreator.createBlock(_n);
m_Trials = blockCreator.createBlock(_n);
_score.startBlock(_n);

// present the first trial to the user
presentTrialInfoToUser(m_Trials[_trialNum]);
self.presentTrialInfoToUser(m_Trials[_trialNum]);
_score.startNewTrial(m_Trials[_trialNum].GetSecondTrialInTarget);

// start the timers for the first trial
Expand Down Expand Up @@ -239,29 +261,29 @@ function Page() {

// start a new trial
_score.startNewTrial(m_Trials[_trialNum].GetSecondTrialInTarget());
presentTrialInfoToUser(m_Trials[_trialNum]);
self.presentTrialInfoToUser(m_Trials[_trialNum]);

//Timer_1.Begin();
//TrialTimer.Begin();
}

this.startBlock() {
this.startBlock = function() {
// start a new trial
_score.startNewTrial(m_Trials[_trialNum].GetSecondTrialInTarget());
presentTrialInfoToUser(m_Trials[_trialNum]);
self.presentTrialInfoToUser(m_Trials[_trialNum]);

//Timer_1.Seek(new TimeSpan(0));
//TrailTimer.Seek(new TimeSpan(0));
//Timer_1.Begin();
//TrialTimer.Begin();
}

this.handleScores(totalCorrect, totalScore) {
this.handleScores = function(totalCorrect, totalScore) {
//ScoreText.Text = totalScore.ToString();
}

// display trial information (square, audio, etc.) to the user
this.presentTrialInfoToUser(t) {
this.presentTrialInfoToUser = function(t) {
switch(t.GetLetter()) {
case Consonant.Letter1:
// play audio...
Expand Down Expand Up @@ -343,7 +365,7 @@ function Page() {
iRow = 0;
break;

case default:
default:
break;
}

Expand Down
8 changes: 0 additions & 8 deletions js/Score.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
var TrialResult = {
Audio_Success: 1,
Audio_Failure: 2,
Visual_Success: 3,
Visual_Failure: 4,
};


// Class that keeps track of the user's score
function Score() {
var _target = TargetKind.None;
Expand Down
11 changes: 11 additions & 0 deletions js/SquarePosition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// enum of possible positions of the square as it's displayed to the user
var SquarePosition = {
TopLeft: 0,
TopMiddle: 1,
TopRight: 2,
MiddleRight: 3,
BottomRight: 4,
BottomMiddle: 5,
BottomLeft: 6,
MiddleLeft: 7,
};
8 changes: 8 additions & 0 deletions js/TargetKind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// enum of the kinds of target of each trial
var TargetKind = {
Audio: 1,
Visual: 2,
Both: 3,
None: 4, // the trial doesn't match
TooEarly: 5, // this trial is too early in the list, and it doesn't have an earlier pair
};
34 changes: 0 additions & 34 deletions js/Trial.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,3 @@
// enum of possible positions of the square as it's displayed to the user
var SquarePosition = {
TopLeft: 0,
TopMiddle: 1,
TopRight: 2,
MiddleRight: 3,
BottomRight: 4,
BottomMiddle: 5,
BottomLeft: 6,
MiddleLeft: 7,
};

// enum of possible consonants to be played to the user
var Consonant = {
Letter1: 0,
Letter2: 1,
Letter3: 2,
Letter4: 3,
Letter5: 4,
Letter6: 5,
Letter7: 6,
Letter8: 7,
};

// enum of the kinds of target of each trial
var TargetKind = {
Audio: 1,
Visual: 2,
Both: 3,
None: 4, // the trial doesn't match
TooEarly: 5, // this trial is too early in the list, and it doesn't have an earlier pair
};


function Trial(position, cons) {
this._position = position;
this._letter = cons;
Expand Down
6 changes: 6 additions & 0 deletions js/TrialResult.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var TrialResult = {
Audio_Success: 1,
Audio_Failure: 2,
Visual_Success: 3,
Visual_Failure: 4,
};
Loading

0 comments on commit 9f260e7

Please sign in to comment.