Created
September 17, 2019 09:55
-
-
Save severuykhin/0635f7641ad3c74bf04ca55e0d1d9afc to your computer and use it in GitHub Desktop.
// source https://jsbin.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
const Condition = { | |
id: "int(11)", | |
question_id: "int(11)", | |
type: "varchar(3)" | |
} | |
const ConditionItem = { | |
id: "int(11)", | |
condition_id: "int(11)", | |
parent_question_id: "int(11)", | |
answer_index: "int(11)" | |
} | |
const conditions = { | |
1920: null, | |
1921: { | |
type: 'AND', | |
conditions: [ | |
{ | |
questionId: 1920, | |
answerIndex: 1 | |
} | |
] | |
}, | |
1922: { | |
type: 'AND', | |
conditions: [ | |
{ | |
questionId: 1920, | |
answerIndex: 1 | |
}, | |
{ | |
questionId: 1921, | |
answerIndex: 2 | |
} | |
] | |
} | |
} | |
const answeredQuestions = { | |
1920: [1], | |
1921: [1,4] | |
} | |
const summResults = (results, operator) => { | |
let summ = results.reduce((prev, next) => prev + Number(next), 0); | |
if (operator === 'OR') { | |
return summ > 0; | |
} else { | |
return summ === results.length; | |
} | |
} | |
/** | |
* | |
* @param {number} questionId | |
* @param {object} conditions | |
* @param {object} answeredQuestions | |
*/ | |
const resolveShowOrNot = (questionId, conditions, answeredQuestions) => { | |
const questionConditions = conditions[questionId]; | |
if (!questionConditions) return true; | |
let results = []; | |
questionConditions.conditions.forEach((condition, index) => { | |
let conditionQustionAnswers = answeredQuestions[condition.questionId]; | |
if (conditionQustionAnswers.indexOf(condition.answerIndex) >= 0) { | |
results.push(true); | |
} else { | |
results.push(false); | |
} | |
}); | |
if (results.length === 1) return results[0]; | |
return summResults(results, questionConditions.type); | |
} | |
// Test summResult func | |
console.log(summResults([true,true,true], 'AND') === true); | |
console.log(summResults([false,false,false], 'OR') === false); | |
console.log(summResults([false,false,false], 'AND') === false); | |
console.log(summResults([true, true], 'OR') === true); | |
console.log(summResults([true, false], 'AND') === false); | |
console.log('------'); | |
// Test resolve show or not func | |
console.log(resolveShowOrNot(1921, conditions, answeredQuestions)); | |
console.log(resolveShowOrNot(1922, conditions, answeredQuestions)); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const Condition = { | |
id: "int(11)", | |
question_id: "int(11)", | |
type: "varchar(3)" | |
} | |
const ConditionItem = { | |
id: "int(11)", | |
condition_id: "int(11)", | |
parent_question_id: "int(11)", | |
answer_index: "int(11)" | |
} | |
const conditions = { | |
1920: null, | |
1921: { | |
type: 'AND', | |
conditions: [ | |
{ | |
questionId: 1920, | |
answerIndex: 1 | |
} | |
] | |
}, | |
1922: { | |
type: 'AND', | |
conditions: [ | |
{ | |
questionId: 1920, | |
answerIndex: 1 | |
}, | |
{ | |
questionId: 1921, | |
answerIndex: 2 | |
} | |
] | |
} | |
} | |
const answeredQuestions = { | |
1920: [1], | |
1921: [1,4] | |
} | |
const summResults = (results, operator) => { | |
let summ = results.reduce((prev, next) => prev + Number(next), 0); | |
if (operator === 'OR') { | |
return summ > 0; | |
} else { | |
return summ === results.length; | |
} | |
} | |
/** | |
* | |
* @param {number} questionId | |
* @param {object} conditions | |
* @param {object} answeredQuestions | |
*/ | |
const resolveShowOrNot = (questionId, conditions, answeredQuestions) => { | |
const questionConditions = conditions[questionId]; | |
if (!questionConditions) return true; | |
let results = []; | |
questionConditions.conditions.forEach((condition, index) => { | |
let conditionQustionAnswers = answeredQuestions[condition.questionId]; | |
if (conditionQustionAnswers.indexOf(condition.answerIndex) >= 0) { | |
results.push(true); | |
} else { | |
results.push(false); | |
} | |
}); | |
if (results.length === 1) return results[0]; | |
return summResults(results, questionConditions.type); | |
} | |
// Test summResult func | |
console.log(summResults([true,true,true], 'AND') === true); | |
console.log(summResults([false,false,false], 'OR') === false); | |
console.log(summResults([false,false,false], 'AND') === false); | |
console.log(summResults([true, true], 'OR') === true); | |
console.log(summResults([true, false], 'AND') === false); | |
console.log('------'); | |
// Test resolve show or not func | |
console.log(resolveShowOrNot(1921, conditions, answeredQuestions)); | |
console.log(resolveShowOrNot(1922, conditions, answeredQuestions));</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Condition = { | |
id: "int(11)", | |
question_id: "int(11)", | |
type: "varchar(3)" | |
} | |
const ConditionItem = { | |
id: "int(11)", | |
condition_id: "int(11)", | |
parent_question_id: "int(11)", | |
answer_index: "int(11)" | |
} | |
const conditions = { | |
1920: null, | |
1921: { | |
type: 'AND', | |
conditions: [ | |
{ | |
questionId: 1920, | |
answerIndex: 1 | |
} | |
] | |
}, | |
1922: { | |
type: 'AND', | |
conditions: [ | |
{ | |
questionId: 1920, | |
answerIndex: 1 | |
}, | |
{ | |
questionId: 1921, | |
answerIndex: 2 | |
} | |
] | |
} | |
} | |
const answeredQuestions = { | |
1920: [1], | |
1921: [1,4] | |
} | |
const summResults = (results, operator) => { | |
let summ = results.reduce((prev, next) => prev + Number(next), 0); | |
if (operator === 'OR') { | |
return summ > 0; | |
} else { | |
return summ === results.length; | |
} | |
} | |
/** | |
* | |
* @param {number} questionId | |
* @param {object} conditions | |
* @param {object} answeredQuestions | |
*/ | |
const resolveShowOrNot = (questionId, conditions, answeredQuestions) => { | |
const questionConditions = conditions[questionId]; | |
if (!questionConditions) return true; | |
let results = []; | |
questionConditions.conditions.forEach((condition, index) => { | |
let conditionQustionAnswers = answeredQuestions[condition.questionId]; | |
if (conditionQustionAnswers.indexOf(condition.answerIndex) >= 0) { | |
results.push(true); | |
} else { | |
results.push(false); | |
} | |
}); | |
if (results.length === 1) return results[0]; | |
return summResults(results, questionConditions.type); | |
} | |
// Test summResult func | |
console.log(summResults([true,true,true], 'AND') === true); | |
console.log(summResults([false,false,false], 'OR') === false); | |
console.log(summResults([false,false,false], 'AND') === false); | |
console.log(summResults([true, true], 'OR') === true); | |
console.log(summResults([true, false], 'AND') === false); | |
console.log('------'); | |
// Test resolve show or not func | |
console.log(resolveShowOrNot(1921, conditions, answeredQuestions)); | |
console.log(resolveShowOrNot(1922, conditions, answeredQuestions)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment