Skip to content

Commit

Permalink
Replaces hash function with btoa() for questionToId key encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
clpetersonucf committed Jan 30, 2020
1 parent 8719816 commit d18b113
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,10 @@ SortItOut.controller("SortItOutEngineCtrl", ["$scope", "$rootScope", "$timeout",
$scope.$apply()
}

// hash function to be used in conjunction with questionToId
const hash = (str) => {
let hash = 0, i, char
if (str.length == 0) return hash
for (i = 0; i < str.length; i++) {
char = str.charCodeAt(i)
hash = ((hash << 5) - hash) + char
hash |= 0
}
return Math.abs(hash)
}

const generateQuestionToId = qset => {
for (let item of qset.items) {
questionToId.set(hash(sanitizeHelper.desanitize(item.questions[0].text)), item.id)
// btoa encodes question text as a base64 string to prevent failure from special characters
questionToId.set(btoa(sanitizeHelper.desanitize(item.questions[0].text)), item.id)
}
}

Expand Down Expand Up @@ -497,7 +486,7 @@ SortItOut.controller("SortItOutEngineCtrl", ["$scope", "$rootScope", "$timeout",

$scope.folders.forEach( ({text, items}) => {
items.forEach( item => {
const id = questionToId.get(hash(item.text))
const id = questionToId.get(btoa(item.text))
Materia.Score.submitQuestionForScoring(id, text)
})
})
Expand Down

0 comments on commit d18b113

Please sign in to comment.