allow negative values in numeric quiz question

fixes QO-376

test plan:
 - Add a numeric question to a quiz
 - change the question answer type to "Answer with Precision"
 - Change Precision to a value between 1 and 15
 - Type a negative answer into the answer field (or a zero)
 - The negative value should be formatted with the correct
   precision on blur (it should not change to NaN)
 - Students should be able to answer the question correctly

Change-Id: Iabf33557ca4544febe121c74df12837e5a73ed06
Reviewed-on: https://gerrit.instructure.com/156252
Tested-by: Jenkins
Reviewed-by: Mark Grant <mgrant@instructure.com>
QA-Review: Nathan Gbedemah <ngbedemah@instructure.com>
Product-Review: Kevin Dougherty <jdougherty@instructure.com>
This commit is contained in:
Jared Crystal 2018-07-05 13:18:49 -06:00
parent 46832c19dd
commit 810674d62b
1 changed files with 14 additions and 11 deletions

View File

@ -941,7 +941,7 @@ const lockedItems = lockManager.isChildContent() ? lockManager.getItemLocks() :
return null;
},
parseInput: function($input, type) {
parseInput: function($input, type, precision = 10) {
if ($input.val() == "") { return; }
var val = $input.val().replace(/,/g, '');
@ -960,17 +960,20 @@ const lockedItems = lockManager.isChildContent() ? lockManager.getItemLocks() :
val = parseFloat(val)
if (isNaN(val)) { val = 0.0; }
// Round to precision 16 to handle floating point error
val = val.toPrecision(16);
// Truncate to specified precision
var precision = arguments[2] || 10;
if (precision < 16) {
var precision_shift = Math.pow(10, precision - Math.floor(Math.log(val) / Math.LN10) - 1);
val = Math.floor(val * precision_shift) / precision_shift;
// Format
if (val === 0) {
val = val.toPrecision(precision)
} else {
// Round to precision 16 to handle floating point error
val = val.toPrecision(16);
// Truncate to specified precision
if (precision < 16) {
const precision_shift = 10 ** (precision - Math.floor(Math.log(Math.abs(val)) / Math.LN10) - 1);
val = Math.floor(val * precision_shift) / precision_shift;
// Format
val = val.toPrecision(precision)
}
}
}