Solution 05 (all exercises)

Solution: make the loop count down from 5 to 1

for (var i=5; i>0; i--) {
    window.alert("The number is "+i);
}

Solution: an experiment which loops through an array of strings

var jsPsych = initJsPsych({
    on_finish: function() {
        jsPsych.data.displayData();
    }
});

var sentence = ["I", "wandered", "lonely", "as", "a", "cloud"];

var trials = [];

for (var word of sentence) {
  var trial = {
      type: jsPsychHtmlKeyboardResponse,
      prompt: '<p>Press a key!</p>',
      stimulus: word
  };
  trials.push(trial);
}

// add a list of all images, these will be loaded right at the start
// to avoid delays
jsPsych.run(trials);

You may have spotted that you could also do this using timeline variables!

Solution: greetings

function greeting(person) {
    window.alert("hello "+person+"!");
}

var names = ['Hendrick', 'Arran', 'Kalvyn', 'Priyangi', 'Ted'];

for (var name of names) {
    greeting(name);
}