|
@@ -53,9 +53,16 @@ function Subtraction() {
|
|
carryArrCopy.unshift(e.target.value);
|
|
carryArrCopy.unshift(e.target.value);
|
|
setCarryArr(carryArrCopy);
|
|
setCarryArr(carryArrCopy);
|
|
let noCarry = carryArrCopy[0] === "0" || carryArrCopy[0] === undefined;
|
|
let noCarry = carryArrCopy[0] === "0" || carryArrCopy[0] === undefined;
|
|
- if(nosLeft === 0 || (nosLeft === 1 && noCarry) ||
|
|
|
|
- // stop after first iteration, numbers left is undefined at first
|
|
|
|
|
|
+
|
|
|
|
+ if((nosLeft === 1 && noCarry) ||
|
|
|
|
+ // if stop after first iteration, numbers left is undefined at first
|
|
(nosLeft !== nosLeft && realResult.toString().length === 1 && noCarry)){
|
|
(nosLeft !== nosLeft && realResult.toString().length === 1 && noCarry)){
|
|
|
|
+ nosLeft = 0;
|
|
|
|
+ handleResChange(" ", 0)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(nosLeft === 0){
|
|
|
|
+ createIdmtResults()
|
|
setShowAlert(true);
|
|
setShowAlert(true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -76,7 +83,7 @@ function Subtraction() {
|
|
let afterComma = Math.max(...numbers.map(x => afterCommaLen(x)));
|
|
let afterComma = Math.max(...numbers.map(x => afterCommaLen(x)));
|
|
realRes = parseFloat(realRes.toFixed(afterComma));
|
|
realRes = parseFloat(realRes.toFixed(afterComma));
|
|
setRealResult(realRes);
|
|
setRealResult(realRes);
|
|
- console.log("real result: ", realRes)
|
|
|
|
|
|
+ console.log("real result: ", realRes);
|
|
|
|
|
|
let [numbersArr, commaIdx] = numbersToArrOfArr(numbers);
|
|
let [numbersArr, commaIdx] = numbersToArrOfArr(numbers);
|
|
setNumbers(numbersArr);
|
|
setNumbers(numbersArr);
|
|
@@ -96,12 +103,17 @@ function Subtraction() {
|
|
let digit = 0;
|
|
let digit = 0;
|
|
|
|
|
|
if(typeof numbers === "object" && imdtResIdx !== -1){
|
|
if(typeof numbers === "object" && imdtResIdx !== -1){
|
|
|
|
+ // set the digit index, start with the last digit
|
|
if(typeof imdtResIdx === "undefined" || imdtResIdx === null){
|
|
if(typeof imdtResIdx === "undefined" || imdtResIdx === null){
|
|
noOfDigits = Math.min(...numbers.map(n => n.length));
|
|
noOfDigits = Math.min(...numbers.map(n => n.length));
|
|
- imdtResIdx = noOfDigits-1;
|
|
|
|
|
|
+ imdtResIdx = noOfDigits - 1;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // skip over comma
|
|
if(imdtResIdx === commaIdx){
|
|
if(imdtResIdx === commaIdx){
|
|
- handleResChange(".", nosLeft)
|
|
|
|
|
|
+ if(!resArr.includes(".")){
|
|
|
|
+ handleResChange(".", nosLeft);
|
|
|
|
+ }
|
|
imdtResIdx = imdtResIdx - 1;
|
|
imdtResIdx = imdtResIdx - 1;
|
|
nosLeft = nosLeft - 1;
|
|
nosLeft = nosLeft - 1;
|
|
}
|
|
}
|
|
@@ -110,22 +122,24 @@ function Subtraction() {
|
|
labelText = "("
|
|
labelText = "("
|
|
}
|
|
}
|
|
|
|
|
|
- for (let n in numbers){ // iterate numbers
|
|
|
|
|
|
+ // iterate numbers for this digit index
|
|
|
|
+ for (let n in numbers){
|
|
if(parseInt(n)===0){
|
|
if(parseInt(n)===0){
|
|
firstDigit = numbers[parseInt(n)][imdtResIdx];
|
|
firstDigit = numbers[parseInt(n)][imdtResIdx];
|
|
}else{
|
|
}else{
|
|
|
|
+ // skip last empty number if no carry
|
|
if(nosLeft === 1 && carryArr[0] === "0"){
|
|
if(nosLeft === 1 && carryArr[0] === "0"){
|
|
nosLeft = 0;
|
|
nosLeft = 0;
|
|
imdtResIdx = -1;
|
|
imdtResIdx = -1;
|
|
- handleResChange(" ", 0)
|
|
|
|
return <></>
|
|
return <></>
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // create label text like: d + d + (wie viel) = input
|
|
digit = numbers[parseInt(n)][imdtResIdx];
|
|
digit = numbers[parseInt(n)][imdtResIdx];
|
|
if(digit === "" || digit === " "){
|
|
if(digit === "" || digit === " "){
|
|
digit = 0;
|
|
digit = 0;
|
|
}
|
|
}
|
|
labelText += digit;
|
|
labelText += digit;
|
|
-
|
|
|
|
if (parseInt(n) === numbers.length - 1){
|
|
if (parseInt(n) === numbers.length - 1){
|
|
if(carryArr[0] !== undefined && carryArr[0] > 0){
|
|
if(carryArr[0] !== undefined && carryArr[0] > 0){
|
|
labelText += " + " + carryArr[0].toString();
|
|
labelText += " + " + carryArr[0].toString();
|
|
@@ -142,11 +156,8 @@ function Subtraction() {
|
|
labelText += " + wie viel = " + firstDigit
|
|
labelText += " + wie viel = " + firstDigit
|
|
labelText = labelText.replace(/ /g, "0")
|
|
labelText = labelText.replace(/ /g, "0")
|
|
|
|
|
|
- if(carryArr[0] > -1 || imdtResIdx === noOfDigits-1){
|
|
|
|
- imdtRes = -1;
|
|
|
|
- imdtResIdx = imdtResIdx - 1;
|
|
|
|
- nosLeft = nosLeft - 1;
|
|
|
|
- }
|
|
|
|
|
|
+ imdtResIdx = imdtResIdx - 1;
|
|
|
|
+ nosLeft = nosLeft - 1;
|
|
|
|
|
|
return (
|
|
return (
|
|
<form>
|
|
<form>
|
|
@@ -155,7 +166,7 @@ function Subtraction() {
|
|
</label>
|
|
</label>
|
|
<input
|
|
<input
|
|
onChange={(e) => handleResChange(e, nosLeft)}
|
|
onChange={(e) => handleResChange(e, nosLeft)}
|
|
- type="text" id="input_result" size="2"
|
|
|
|
|
|
+ type="text" id="input_result" size="2" maxLength="1"
|
|
aria-labelledby="input_result_label" aria-required="true"
|
|
aria-labelledby="input_result_label" aria-required="true"
|
|
ref={resInputField} autoFocus/>
|
|
ref={resInputField} autoFocus/>
|
|
<br/>
|
|
<br/>
|
|
@@ -164,7 +175,7 @@ function Subtraction() {
|
|
</label>
|
|
</label>
|
|
<input
|
|
<input
|
|
onChange={(e) => handleCarryChange(e, noOfDigits, nosLeft)}
|
|
onChange={(e) => handleCarryChange(e, noOfDigits, nosLeft)}
|
|
- type="text" id="input_carry" size="2"
|
|
|
|
|
|
+ type="text" id="input_carry" size="2" maxLength="1"
|
|
aria-labelledby="input_carry_label" aria-required="true"
|
|
aria-labelledby="input_carry_label" aria-required="true"
|
|
ref={carryInputField}/>
|
|
ref={carryInputField}/>
|
|
</form>
|
|
</form>
|
|
@@ -177,8 +188,32 @@ function Subtraction() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ const addButtonsToImdtSteps = () => {
|
|
|
|
+ if(stepsGrid[0]["step"]!==""){
|
|
|
|
+ let table = document.getElementById("idmtResultSteps").getElementsByTagName("table")[0]
|
|
|
|
+ let trs = table.getElementsByTagName("tr");
|
|
|
|
+ for(let trIdx in trs){
|
|
|
|
+ let tr = trs[trIdx];
|
|
|
|
+ if(tr instanceof HTMLElement && trIdx < trs.length-1){
|
|
|
|
+ let td = tr.getElementsByTagName("td")[0];
|
|
|
|
+ let btn = document.createElement("button");
|
|
|
|
+ btn.innerHTML = "hier starten";
|
|
|
|
+ btn.classList = "btn btn-secondary btn-sm";
|
|
|
|
+ btn.id = "btn-" + trIdx;
|
|
|
|
+ btn.addEventListener("click", () => startOver(trIdx));
|
|
|
|
+ td.appendChild(btn);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
const showIdmtResults = () => {
|
|
const showIdmtResults = () => {
|
|
setShowAlert(false);
|
|
setShowAlert(false);
|
|
|
|
+ document.getElementById("idmtResultSteps").style.display = "inline-block";
|
|
|
|
+ addButtonsToImdtSteps()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ const createIdmtResults = () => {
|
|
let realResArr = realResult.toString().split("");
|
|
let realResArr = realResult.toString().split("");
|
|
while(realResArr.length < resArr.length){
|
|
while(realResArr.length < resArr.length){
|
|
realResArr.unshift(" "); // add " " before
|
|
realResArr.unshift(" "); // add " " before
|
|
@@ -259,7 +294,7 @@ function Subtraction() {
|
|
text += resArr[idxNumbers] + " = " + firstDigit
|
|
text += resArr[idxNumbers] + " = " + firstDigit
|
|
text += " mit " + carries[idxCarry-1] + " Übertrag "
|
|
text += " mit " + carries[idxCarry-1] + " Übertrag "
|
|
text = text.replace(/ /g, "0")
|
|
text = text.replace(/ /g, "0")
|
|
- text += resArr[idxNumbers]===realDigitRes && carries[idxCarry-1]===realCarry ? "(Richtig)" : "(Falsch)";
|
|
|
|
|
|
+ text += resArr[idxNumbers]===realDigitRes && carries[idxCarry-1]===realCarry ? "(Richtig) " : "(Falsch) ";
|
|
stepsGridCopy.push({step: text});
|
|
stepsGridCopy.push({step: text});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -269,7 +304,42 @@ function Subtraction() {
|
|
let btnSubmit = document.createElement("button");
|
|
let btnSubmit = document.createElement("button");
|
|
btnSubmit.innerHTML = "Ergebnis abgeben";
|
|
btnSubmit.innerHTML = "Ergebnis abgeben";
|
|
btnSubmit.addEventListener("click", finishCalculation);
|
|
btnSubmit.addEventListener("click", finishCalculation);
|
|
|
|
+ btnSubmit.classList = "btn btn-secondary btn-sm";
|
|
|
|
+ btnSubmit.id = "btnSubmitSteps";
|
|
document.getElementById("idmtResultSteps").appendChild(btnSubmit);
|
|
document.getElementById("idmtResultSteps").appendChild(btnSubmit);
|
|
|
|
+
|
|
|
|
+ document.getElementById("idmtResultSteps").tabIndex = 0;
|
|
|
|
+ document.getElementById("idmtResultSteps").focus();
|
|
|
|
+
|
|
|
|
+ document.getElementById("idmtResultSteps").style.display = "none";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ const startOver = (idx) => {
|
|
|
|
+ let invertedIdx = numbers[0].length - idx - 1;
|
|
|
|
+ imdtResIdx = invertedIdx;
|
|
|
|
+
|
|
|
|
+ let carryArrCopy = [...carryArr];
|
|
|
|
+ let slicer = invertedIdx;
|
|
|
|
+ if(resArr[0] !== " " && !resArr.includes(".")){
|
|
|
|
+ slicer += 1;
|
|
|
|
+ }else if(resArr[0] === " " && resArr.includes(".")){
|
|
|
|
+ slicer -= 1;
|
|
|
|
+ }
|
|
|
|
+ carryArrCopy = carryArrCopy.slice(slicer);
|
|
|
|
+
|
|
|
|
+ // reset result and carries until the chosen step
|
|
|
|
+ let commaPos = resArr.indexOf(".");
|
|
|
|
+ if(resArr.includes(".") && commaPos >= invertedIdx){
|
|
|
|
+ imdtResIdx -= 1;
|
|
|
|
+ resArr = resArr.slice(invertedIdx);
|
|
|
|
+ }else{
|
|
|
|
+ resArr = resArr.slice(invertedIdx + 1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ setCarryArr(carryArrCopy);
|
|
|
|
+
|
|
|
|
+ document.getElementById("btnSubmitSteps").remove();
|
|
|
|
+ document.getElementById("idmtResultSteps").style.display = "none";
|
|
}
|
|
}
|
|
|
|
|
|
const finishCalculation = () => {
|
|
const finishCalculation = () => {
|
|
@@ -363,6 +433,7 @@ function Subtraction() {
|
|
focusedRowEnabled={true}
|
|
focusedRowEnabled={true}
|
|
showBorders={true}
|
|
showBorders={true}
|
|
showColumnHeaders={false}
|
|
showColumnHeaders={false}
|
|
|
|
+ noDataText=""
|
|
>
|
|
>
|
|
<Column dataField="step" />
|
|
<Column dataField="step" />
|
|
</DataGrid>
|
|
</DataGrid>
|