Skip to content

Instantly share code, notes, and snippets.

@hieptuanle
Created July 21, 2016 09:53
Show Gist options
  • Save hieptuanle/439f0d4a9adb403c4cc25593e56e7053 to your computer and use it in GitHub Desktop.
Save hieptuanle/439f0d4a9adb403c4cc25593e56e7053 to your computer and use it in GitHub Desktop.
const debug = _.curry((tag, x) => {
console.log(tag, x)
return x
})
$http.get('/quizzes/' + $stateParams.quizId + '/do')
.success(function (response) {
$scope.paper = response.paper
const questions = response.paper.questions
// getAnswerse :: Question -> Answer[]
const getAnswers = _.property('question.answers')
// getRightAnswer :: Answer[] -> Answer
const getRightAnswer = answers => { return _.find(answers, {isKey: true}) }
// getAnswerContent :: Answer[] -> String
const getAnswerContent = _.property('content')
// getContentFromQuestion :: Question -> String
const getContentFromQuestion = _.compose(debug('getRightAnswer'), getAnswerContent, debug('getRightAnswer'), getRightAnswer,debug('getAnswers'), getAnswers)
// setRightAnswerContent :: Question -> QuestionWithRightAnswer
const getQuestionWithRightAnswer = _.curry((question, content) => _.assign({}, question, {answers: content}))
$scope.paper.questions = questions.map(question => getQuestionWithRightAnswer(question, getContentFromQuestion(question)))
console.log('$scope.paper.questions', $scope.paper.questions)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment