Skip to content

Commit

Permalink
Timing Related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tafakkur committed Jan 13, 2021
1 parent 4b08443 commit 6694bb7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
37 changes: 37 additions & 0 deletions JavaScript Files/Getting the opening time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Qualtrics.SurveyEngine.addOnReady(function () {
rows = this.getChoiceContainer().querySelectorAll("tr.ChoiceRow");
summer = (sum, val) => sum + val;

input_boxes = [];
for (i = 0; i < rows.length; i++) {
bx = rows[i].querySelectorAll("input");
for (j = 0; j < bx.length; j++) {
input_boxes.push(bx[j]);
}
}
input_boxes.forEach((item) => item.addEventListener("input", get_hours));

function get_hours() {
open_for = [];
for (i = 0; i < rows.length; i++) {
tot = [];
for (j = 0; j < 2; j++) {
m = i * 2 + j;
t = input_boxes[m].value.split(":");
timing = Number(t[0]) * 60 + Number(t[1]);
tot.push(timing);
}
open_for.push(tot[1] - tot[0]);
}

open_hours = (open_for.reduce(summer) / 60).floor();
open_mins = open_for.reduce(summer) - (open_hours * 60) ;

open_text = open_hours + " Hours";
if(open_mins){
open_text += " and " + open_mins + " minutes";
}

Qualtrics.SurveyEngine.setEmbeddedData("hours_open", open_text);
}
});
41 changes: 41 additions & 0 deletions functions/Timing Calculation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Qualtrics.SurveyEngine.addOnReady(function () {
rows = this.getChoiceContainer().querySelectorAll("tr");

input_boxes = [];
display_boxes = [];
for (i = 1; i < rows.length; i++) {
bx = rows[i].querySelectorAll("input");
for (j = 0; j < bx.length - 1; j++) {
input_boxes.push(bx[j]);
}
display_boxes.push(bx[bx.length - 1]);
}
display_boxes.forEach((item) => {
item.disable();
item.style.width = "200px";
});

per_update = input_boxes.length / display_boxes.length;
function update_value() {
for (i = 0; i < display_boxes.length; i++) {
tot = [];
for (j = 0; j < per_update; j++) {
m = i * per_update + j;
t = input_boxes[m].value.split(":");
timing = Number(t[0]) * 60 + Number(t[1]);
tot.push(timing);
}
ot = (tot[1] - tot[0]) / 60;
open_hrs = ot.floor();
open_mins = ((ot - open_hrs) * 60).round();
display_boxes[i].value =
open_hrs + " Hours and " + open_mins + " Minutes";

if (isNaN(open_hrs) || isNaN(open_mins)) {
display_boxes[i].value = "";
}
}
}

input_boxes.forEach((item) => item.addEventListener("input", update_value));
});

0 comments on commit 6694bb7

Please sign in to comment.