|
@@ -45,19 +45,19 @@ function Subtraction() {
|
|
|
}else{
|
|
|
// add input number to result array
|
|
|
resArr.unshift(imdtRes);
|
|
|
- if(nosLeft === 0){
|
|
|
- setShowAlert(true);
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- const handleCarryChange = (e, noOfDigits, idx) => {
|
|
|
- if(typeof carryArr === "undefined"){
|
|
|
- setCarryArr(Array(noOfDigits).fill("-"));
|
|
|
- }
|
|
|
- let carryArrCopy = [...carryArr]
|
|
|
- carryArrCopy[idx] = e.target.value
|
|
|
+ const handleCarryChange = (e, noOfDigits, nosLeft) => {
|
|
|
+ let carryArrCopy = [...carryArr];
|
|
|
+ carryArrCopy.unshift(e.target.value);
|
|
|
setCarryArr(carryArrCopy);
|
|
|
+ let noCarry = carryArrCopy[0] === "0" || carryArrCopy[0] === undefined;
|
|
|
+ if(nosLeft === 0 || (nosLeft === 1 && noCarry) ||
|
|
|
+ // stop after first iteration, numbers left is undefined at first
|
|
|
+ (nosLeft !== nosLeft && realResult.toString().length === 1 && noCarry)){
|
|
|
+ setShowAlert(true);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
const handleSubmit = (e) => {
|
|
@@ -76,6 +76,7 @@ function Subtraction() {
|
|
|
let afterComma = Math.max(...numbers.map(x => afterCommaLen(x)));
|
|
|
realRes = parseFloat(realRes.toFixed(afterComma));
|
|
|
setRealResult(realRes);
|
|
|
+ console.log("real result: ", realRes)
|
|
|
|
|
|
let [numbersArr, commaIdx] = numbersToArrOfArr(numbers);
|
|
|
setNumbers(numbersArr);
|
|
@@ -91,7 +92,6 @@ function Subtraction() {
|
|
|
let labelText = "";
|
|
|
let nosLeft = imdtResIdx + 1;
|
|
|
let carryText = "Übertrag = ";
|
|
|
- let carryOffset = 0;
|
|
|
let firstDigit = 0;
|
|
|
let digit = 0;
|
|
|
|
|
@@ -103,7 +103,7 @@ function Subtraction() {
|
|
|
if(imdtResIdx === commaIdx){
|
|
|
handleResChange(".", nosLeft)
|
|
|
imdtResIdx = imdtResIdx - 1;
|
|
|
- carryOffset = 1;
|
|
|
+ nosLeft = nosLeft - 1;
|
|
|
}
|
|
|
|
|
|
if(numbers.length>2){
|
|
@@ -127,8 +127,8 @@ function Subtraction() {
|
|
|
labelText += digit;
|
|
|
|
|
|
if (parseInt(n) === numbers.length - 1){
|
|
|
- if(carryArr[imdtResIdx+carryOffset]!=="undefind" && carryArr[imdtResIdx+carryOffset] > 0){
|
|
|
- labelText += " + " + carryArr[imdtResIdx+carryOffset].toString();
|
|
|
+ if(carryArr[0] !== undefined && carryArr[0] > 0){
|
|
|
+ labelText += " + " + carryArr[0].toString();
|
|
|
}
|
|
|
if(numbers.length>2){
|
|
|
labelText += ")";
|
|
@@ -142,7 +142,7 @@ function Subtraction() {
|
|
|
labelText += " + wie viel = " + firstDigit
|
|
|
labelText = labelText.replace(/ /g, "0")
|
|
|
|
|
|
- if(carryArr[imdtResIdx+carryOffset] > -1 || imdtResIdx === noOfDigits-1){
|
|
|
+ if(carryArr[0] > -1 || imdtResIdx === noOfDigits-1){
|
|
|
imdtRes = -1;
|
|
|
imdtResIdx = imdtResIdx - 1;
|
|
|
nosLeft = nosLeft - 1;
|
|
@@ -157,13 +157,13 @@ function Subtraction() {
|
|
|
onChange={(e) => handleResChange(e, nosLeft)}
|
|
|
type="text" id="input_result" size="2"
|
|
|
aria-labelledby="input_result_label" aria-required="true"
|
|
|
- ref={resInputField}/>
|
|
|
+ ref={resInputField} autoFocus/>
|
|
|
<br/>
|
|
|
<label htmlFor="input_carry" id="input_carry_label">
|
|
|
{carryText}
|
|
|
</label>
|
|
|
<input
|
|
|
- onChange={(e) => handleCarryChange(e, noOfDigits, imdtResIdx)}
|
|
|
+ onChange={(e) => handleCarryChange(e, noOfDigits, nosLeft)}
|
|
|
type="text" id="input_carry" size="2"
|
|
|
aria-labelledby="input_carry_label" aria-required="true"
|
|
|
ref={carryInputField}/>
|
|
@@ -192,7 +192,7 @@ function Subtraction() {
|
|
|
|
|
|
let foundComma = false;
|
|
|
let stepsGridCopy = [];
|
|
|
- for (let i=0; i<resArr.length-1; i++) {
|
|
|
+ for (let i=0; i<resArr.length; i++) {
|
|
|
let text = "";
|
|
|
let trueNumbers = false;
|
|
|
let idxNumbers = resArr.length - i - 1;
|
|
@@ -225,7 +225,7 @@ function Subtraction() {
|
|
|
}
|
|
|
|
|
|
// ignore indexes without real digits
|
|
|
- if (resArr[idxNumbers]!==" " || carries[idxCarry]!==" "){
|
|
|
+ if (resArr[idxNumbers]!==" " || (carries[idxCarry]!==" " & carries[idxCarry]!=="0")){
|
|
|
trueNumbers = true;
|
|
|
}
|
|
|
|
|
@@ -290,6 +290,9 @@ function Subtraction() {
|
|
|
}
|
|
|
}
|
|
|
let resCalc = res.join("");
|
|
|
+ if(resCalc.startsWith(".")){
|
|
|
+ resCalc = "0" + resCalc;
|
|
|
+ }
|
|
|
setResultsGrid([{number: resCalc}]);
|
|
|
resCalc = parseFloat(resCalc);
|
|
|
|