|
@@ -97,6 +97,8 @@ function Subtraction() {
|
|
|
let nosLeft = imdtResIdx + 1;
|
|
|
let carryText = "Übertrag = ";
|
|
|
let carryOffset = 0;
|
|
|
+ let firstDigit = 0;
|
|
|
+ let digit = 0;
|
|
|
|
|
|
if(typeof numbers === "object" && imdtResIdx !== -1){
|
|
|
if(typeof imdtResIdx === "undefined" || imdtResIdx === null){
|
|
@@ -109,29 +111,42 @@ function Subtraction() {
|
|
|
carryOffset = 1;
|
|
|
}
|
|
|
|
|
|
- for (let n in numbers){ // iterate numbers
|
|
|
- if(nosLeft === 1 && carryArr[0] === "0"){
|
|
|
- nosLeft = 0;
|
|
|
- imdtResIdx = -1;
|
|
|
- handleResChange(" ", 0)
|
|
|
- return <></>
|
|
|
- }
|
|
|
- let digit = numbers[parseInt(n)][imdtResIdx];
|
|
|
- if(digit === "" || digit === " "){
|
|
|
- digit = 0;
|
|
|
- }
|
|
|
- labelText += digit;
|
|
|
+ if(numbers.length>2){
|
|
|
+ labelText = "("
|
|
|
+ }
|
|
|
|
|
|
- if (parseInt(n) === numbers.length - 1){
|
|
|
- if(carryArr[imdtResIdx+carryOffset]!=="undefind" && carryArr[imdtResIdx+carryOffset] > 0){
|
|
|
- labelText += " + " + carryArr[imdtResIdx+carryOffset].toString();
|
|
|
- }
|
|
|
- labelText += " = ";
|
|
|
+ for (let n in numbers){ // iterate numbers
|
|
|
+ if(parseInt(n)===0){
|
|
|
+ firstDigit = numbers[parseInt(n)][imdtResIdx];
|
|
|
}else{
|
|
|
- labelText += " - ";
|
|
|
+ if(nosLeft === 1 && carryArr[0] === "0"){
|
|
|
+ nosLeft = 0;
|
|
|
+ imdtResIdx = -1;
|
|
|
+ handleResChange(" ", 0)
|
|
|
+ return <></>
|
|
|
+ }
|
|
|
+ digit = numbers[parseInt(n)][imdtResIdx];
|
|
|
+ if(digit === "" || digit === " "){
|
|
|
+ digit = 0;
|
|
|
+ }
|
|
|
+ labelText += digit;
|
|
|
+
|
|
|
+ if (parseInt(n) === numbers.length - 1){
|
|
|
+ if(carryArr[imdtResIdx+carryOffset]!=="undefind" && carryArr[imdtResIdx+carryOffset] > 0){
|
|
|
+ labelText += " + " + carryArr[imdtResIdx+carryOffset].toString();
|
|
|
+ }
|
|
|
+ if(numbers.length>2){
|
|
|
+ labelText += ")";
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ labelText += " + ";
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ labelText += " + wie viel = " + firstDigit
|
|
|
+ labelText = labelText.replace(/ /g, "0")
|
|
|
+
|
|
|
if(carryArr[imdtResIdx+carryOffset] > -1 || imdtResIdx === noOfDigits-1){
|
|
|
imdtRes = -1;
|
|
|
imdtResIdx = imdtResIdx - 1;
|
|
@@ -189,20 +204,28 @@ function Subtraction() {
|
|
|
let idxCarry = carries.length - i - 1;
|
|
|
|
|
|
let realDigitRes = 0;
|
|
|
+ let firstDigit = 0;
|
|
|
for (let j=0; j<numbers.length; j++) {
|
|
|
- let no = numbers[j][idxNumbers];
|
|
|
+ if(parseInt(j)===0){
|
|
|
+ firstDigit = numbers[parseInt(j)][idxNumbers];
|
|
|
+ if(firstDigit==="" | firstDigit===" "){
|
|
|
+ firstDigit = 0;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ let no = numbers[j][idxNumbers];
|
|
|
|
|
|
- if (j === 0){
|
|
|
- realDigitRes = parseInt(no);
|
|
|
- }
|
|
|
- else if (no!=="." && no!==" "){
|
|
|
- trueNumbers = true;
|
|
|
- realDigitRes -= parseInt(no);
|
|
|
- }
|
|
|
+ if (j === 0){
|
|
|
+ realDigitRes = parseInt(no) | 0;
|
|
|
+ }
|
|
|
+ else if (no!=="." && no!==" "){
|
|
|
+ trueNumbers = true;
|
|
|
+ realDigitRes += parseInt(no);
|
|
|
+ }
|
|
|
|
|
|
- text += no
|
|
|
- if (j<numbers.length-1){
|
|
|
- text += " - "
|
|
|
+ text += no
|
|
|
+ if (j<numbers.length-1){
|
|
|
+ text += " + "
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -222,21 +245,24 @@ function Subtraction() {
|
|
|
|
|
|
// add carry only if > 0
|
|
|
if (carries[idxCarry]!=="0"){
|
|
|
- realDigitRes -= parseInt(carries[idxCarry]);
|
|
|
- text += " - " + carries[idxCarry] // + " Übertrag"
|
|
|
+ realDigitRes += parseInt(carries[idxCarry]);
|
|
|
+ text += " + " + carries[idxCarry] // + " Übertrag"
|
|
|
}
|
|
|
- console.log(realDigitRes)
|
|
|
- let realCarry = Math.abs(parseInt(realDigitRes/10)).toString()
|
|
|
- realDigitRes = (realDigitRes % 10)
|
|
|
+ let realCarry = Math.abs(parseInt(realDigitRes/10));
|
|
|
+ realDigitRes = ((firstDigit - realDigitRes) % 10);
|
|
|
+
|
|
|
if (realDigitRes < 0){
|
|
|
- realDigitRes = realDigitRes + 10
|
|
|
+ realDigitRes = realDigitRes + 10;
|
|
|
+ realCarry += 1;
|
|
|
}
|
|
|
+
|
|
|
+ realCarry = realCarry.toString();
|
|
|
realDigitRes = realDigitRes.toString()
|
|
|
- text += " = "
|
|
|
+ text += " und "
|
|
|
|
|
|
if (trueNumbers){
|
|
|
- text += resArr[idxNumbers]
|
|
|
- text += " und " + carries[idxCarry-1] + " Übertrag "
|
|
|
+ text += resArr[idxNumbers] + " = " + firstDigit
|
|
|
+ text += " mit " + carries[idxCarry-1] + " Übertrag "
|
|
|
text = text.replace(/ /g, "0")
|
|
|
text += resArr[idxNumbers]===realDigitRes && carries[idxCarry-1]===realCarry ? "(Richtig)" : "(Falsch)";
|
|
|
stepsGridCopy.push({step: text});
|
|
@@ -253,12 +279,25 @@ function Subtraction() {
|
|
|
const finishCalculation = () => {
|
|
|
setShowAlert(false);
|
|
|
document.getElementById("idmtResultSteps").innerHTML = "";
|
|
|
- let resCalc = resArr.filter(n => n !== " ").join("");
|
|
|
+
|
|
|
+ // remove zeros at the beginning
|
|
|
+ let res = [];
|
|
|
+ let beginNr = false;
|
|
|
+ for(let r in resArr){
|
|
|
+ let digit = resArr[r];
|
|
|
+ if(digit===" "){
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if(digit!=="0" | beginNr | parseInt(r)===resArr.length-1){
|
|
|
+ beginNr = true;
|
|
|
+ res.push(digit)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ let resCalc = res.join("");
|
|
|
setResultsGrid([{number: resCalc}]);
|
|
|
resCalc = parseFloat(resCalc);
|
|
|
|
|
|
let message = "";
|
|
|
- console.log(resCalc, realResult);
|
|
|
if(resCalc === realResult){
|
|
|
message = "Richtig!";
|
|
|
}else{
|