Last active
January 23, 2017 05:15
-
-
Save bdavidxyz/ef29f2e44eafb0da0d304823cc3805e5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* Example : | |
* => Input : | |
* proposals : ['is sky red ?' , 'is sun red ?' , 'is grass red ?' , 'is cloud red ?'] | |
* => Input : | |
* userAnswers : [false, true] | |
* | |
* WARNING : only first(s) userAnswers are given, | |
* all others have implicitly the boolean value "false" | |
* | |
* => Output : | |
* [['is sky red ?', false], | |
* ['is sun red ?', true], | |
* ['is grass red ?', false], | |
* ['are clouds red ?' false]] | |
*/ | |
function labeledCheckboxes (proposals, answers) { | |
// check pre-conditions | |
if (_(proposals).isNotArrayOfString()) return []; | |
if (_(proposals).isEmpty()) return []; | |
if (_(answers).isNotArrayOfBoolean()) return []; | |
if (_(answers).size() > _(proposals).size()) return []; | |
const sizeDifference = _(proposals).size() - _(answers).size(); // 2 | |
const arrayOfFalse = _.times(sizeDifference, _.constant(false)); // [false, false] | |
return _.chain(answers) // [false, true] | |
.concat(arrayOfFalse) // [false, true, false, false] | |
.zip(proposals) // [[false, 'prop 1'], [true, 'prop 2'], [false, 'prop 3'], [false, 'prop 4']] | |
.map(_.reverse) // [['prop 1', false], ['prop 2', true], ['prop 3', false], ['prop 4', false]] | |
.value(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment