From 766054aad3cb09e9bd67c61c9b88dd33123a2353 Mon Sep 17 00:00:00 2001
From: Vukile Langa <vukilelanga@me.com>
Date: Thu, 6 Jun 2024 17:08:17 +0200
Subject: [PATCH] feat(components): negative numbers in numerical queations

---
 .../components/ExactAnswerComponent.js                | 11 ++++++++---
 .../components/PreciseAnswerComponent.js              |  5 +++--
 .../components/RangeAnswerComponent.js                | 10 +++++++---
 3 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/wax-questions-service/src/NumericalAnswerService/components/ExactAnswerComponent.js b/wax-questions-service/src/NumericalAnswerService/components/ExactAnswerComponent.js
index ce36de4d8..586fb979a 100644
--- a/wax-questions-service/src/NumericalAnswerService/components/ExactAnswerComponent.js
+++ b/wax-questions-service/src/NumericalAnswerService/components/ExactAnswerComponent.js
@@ -13,6 +13,7 @@ const ValueContainer = styled.div`
   display: flex;
   flex-direction: column;
   margin-right: 25px;
+
   label {
     font-size: 12px;
   }
@@ -66,7 +67,8 @@ const ExactAnswerComponent = ({ node, readOnly, testMode, showFeedBack }) => {
 
   const onlyNumbers = value => {
     return value
-      .replace(/[^0-9.]/g, '')
+      .replace(/[^-?0-9.]/g, '')
+      .replace(/(?<!^)-/g, '')
       .replace(/(\..*?)\..*/g, '$1')
       .replace(/^0[^.]/, '0');
   };
@@ -124,11 +126,14 @@ const ExactAnswerComponent = ({ node, readOnly, testMode, showFeedBack }) => {
   };
 
   // SUBMIT
-  const exactMultMargin = parseFloat((exact * marginError) / 100);
+  const exactMultMargin = Math.abs(parseFloat((exact * marginError) / 100));
+  const castExactStudent = ['-', '-.', '.'].includes(exactStudent)
+    ? 0
+    : Number(exactStudent);
   const computedMaxValue = Number(exactMultMargin) + Number(exact);
   const computedMinValue = Number(exact) - Number(exactMultMargin);
   const isCorrect = !!(
-    exactStudent <= computedMaxValue && exactStudent >= computedMinValue
+    castExactStudent <= computedMaxValue && castExactStudent >= computedMinValue
   );
 
   return (
diff --git a/wax-questions-service/src/NumericalAnswerService/components/PreciseAnswerComponent.js b/wax-questions-service/src/NumericalAnswerService/components/PreciseAnswerComponent.js
index 84f671787..f4a685487 100644
--- a/wax-questions-service/src/NumericalAnswerService/components/PreciseAnswerComponent.js
+++ b/wax-questions-service/src/NumericalAnswerService/components/PreciseAnswerComponent.js
@@ -13,6 +13,7 @@ const ValueContainer = styled.div`
   display: flex;
   flex-direction: column;
   margin-right: 25px;
+
   label {
     font-size: 12px;
   }
@@ -66,7 +67,7 @@ const PreciseAnswerComponent = ({ node, readOnly, testMode, showFeedBack }) => {
 
   const onlyNumbers = value => {
     return value
-      .replace(/[^0-9.;]/g, '')
+      .replace(/[^-?0-9.;]/g, '')
       .replace(/(\..*?)\..*/g, '$1')
       .replace(/^0[^.]/, '0');
   };
@@ -158,7 +159,7 @@ const PreciseAnswerComponent = ({ node, readOnly, testMode, showFeedBack }) => {
       )}
       {readOnly && showFeedBack && (
         <ResultContainer>
-          <span>{`(Accepted Answers : ${precise.replaceAll(';', ' -')})`}</span>
+          <span>{`(Accepted Answers : ${precise.replaceAll(';', '; ')})`}</span>
           <span>
             Answer:{' '}
             <FinalResult isCorrect={isCorrect}>
diff --git a/wax-questions-service/src/NumericalAnswerService/components/RangeAnswerComponent.js b/wax-questions-service/src/NumericalAnswerService/components/RangeAnswerComponent.js
index 3896b668c..ceced5a0d 100644
--- a/wax-questions-service/src/NumericalAnswerService/components/RangeAnswerComponent.js
+++ b/wax-questions-service/src/NumericalAnswerService/components/RangeAnswerComponent.js
@@ -13,6 +13,7 @@ const ValueContainer = styled.div`
   display: flex;
   flex-direction: column;
   margin-right: 25px;
+
   label {
     font-size: 12px;
   }
@@ -70,7 +71,8 @@ const RangeAnswerComponent = ({ node, readOnly, testMode, showFeedBack }) => {
 
   const onlyNumbers = value => {
     return value
-      .replace(/[^0-9.]/g, '')
+      .replace(/[^-?0-9.]/g, '')
+      .replace(/(?<!^)-/g, '')
       .replace(/(\..*?)\..*/g, '$1')
       .replace(/^0[^.]/, '0');
   };
@@ -128,10 +130,12 @@ const RangeAnswerComponent = ({ node, readOnly, testMode, showFeedBack }) => {
   };
 
   // SUBMIT
+  const castExactStudent = ['-', '-.', '.'].includes(rangeStudentValue)
+    ? 0
+    : Number(rangeStudentValue);
 
   const isCorrect = !!(
-    Number(rangeStudentValue) <= Number(maxValue) &&
-    Number(rangeStudentValue) >= Number(minValue)
+    castExactStudent <= Number(maxValue) && castExactStudent >= Number(minValue)
   );
 
   return (
-- 
GitLab