Matrix constant sum validation | XM Community
Solved

Matrix constant sum validation

  • 15 April 2024
  • 3 replies
  • 34 views

Badge +1

I have a constant sum matrix with three column.

I want to set a validation so that the sum of the 1st column may or may not be 100.

But the column 2 and 3 must be equal to 100.

icon

Best answer by TomG 16 April 2024, 13:15

View original

3 replies

Userlevel 7
Badge +21

You could use a side by side question to add the validations.

 

 

 

And then use the JS below to create the constant sum experience.

 

Qualtrics.SurveyEngine.addOnReady(function () {
const quest = this;
const qc = quest.getQuestionContainer();
const allCells = Array.from(qc.querySelectorAll(".AnswerCell"));
const allCols = [];
allCells
.map((item) =>
Array.from(item.classList).filter((a) => {
if (a.includes("SBS") || a.includes("AnswerCell")) {
return false;
} else {
return a;
}
})
)
.flatten()
.forEach((a) => {
if (allCols.indexOf(a) == -1) {
allCols.push(a);
}
});

const sumClass = allCols.pop();
const selectors = allCols.map((a) => "." + a + " input");

const allRows = Array.from(qc.querySelectorAll("tr.Choice"));

allRows.forEach((row) => {
const answerCell = row.querySelector("." + sumClass + " input");
answerCell.disabled = true;
answerCell.value = 0;

selectors.forEach((col) => {
const input = row.querySelector(col);
input.type = "number";
input.oninput = function () {
updateSum(row);
};
});
});

const updateSum = function (row) {
const allVals = selectors.map((item) => row.querySelector(item).value).map((item) => Number(item) || 0);

const answerCell = row.querySelector("." + sumClass + " input");
answerCell.value = allVals.reduce((a, b) => a + b);
};
});

 

Demo: https://iima.au1.qualtrics.com/jfe/preview/previewId/00a1841e-33a5-49ae-a9c5-6934da853af4/SV_8HBDY4VRJnFn40J/BL_0Mt1Jw9T1Jby9eu?Q_SurveyVersionID=current

 

Userlevel 7
Badge +27

I have a constant sum matrix with three column.

I want to set a validation so that the sum of the 1st column may or may not be 100.

But the column 2 and 3 must be equal to 100.

You can do this with two custom validation conditions, math expressions and piped values. Statement 1-Scale Point 2 should be equal to 100 minus all the other statements for scale point 2 AND Statement 1/Scale Point 3 should be equal to 100 minus all the other statements for scale point 3.

Badge +1

I have a constant sum matrix with three column.

I want to set a validation so that the sum of the 1st column may or may not be 100.

But the column 2 and 3 must be equal to 100.

You can do this with two custom validation conditions, math expressions and piped values. Statement 1-Scale Point 2 should be equal to 100 minus all the other statements for scale point 2 AND Statement 1/Scale Point 3 should be equal to 100 minus all the other statements for scale point 3.

I can see where you’re going with this, but I just wanted to make sure if we’re on the same page.

Since the first column answers are being piped in (Their sum won’t necessarily be equal to 100), I want to make sure that the 2nd and 3rd column’s sum is in fact equal to 100. How can I go about applying validation for this?

Leave a Reply