|
@@ -1,24 +1,25 @@
|
|
|
import React, {useState, useEffect, useRef} from "react";
|
|
|
import DataGrid, {Column} from "devextreme-react/data-grid";
|
|
|
-import {addNumbersToGrid, numbersToArrOfArr, afterCommaLen, handleKeyDown} from "./helpers.js";
|
|
|
+import {numbersToArrOfArr, afterCommaLen, handleKeyDown} from "./helpers.js";
|
|
|
import "./App.css";
|
|
|
|
|
|
let imdtRes;
|
|
|
let imdtResIdx;
|
|
|
let resArr = [];
|
|
|
let noOfDigits = 0;
|
|
|
+let commaIsSet = false;
|
|
|
|
|
|
function Subtraction() {
|
|
|
const [input, setInput] = useState("");
|
|
|
const [realResult, setRealResult] = useState(0);
|
|
|
const [carryArr, setCarryArr] = useState([]);
|
|
|
- const [commaIdx, setCommaIdx] = useState(0);
|
|
|
+ const [commaIdx, setCommaIdx] = useState(-1);
|
|
|
const [numbers, setNumbers] = useState(0);
|
|
|
- const [numbersGrid, setNumbersGrid] = useState([{id: 1, number: ""}]);
|
|
|
- const [resultsGrid, setResultsGrid] = useState([{id: 1, number: ""}]);
|
|
|
- const [stepsGrid, setStepsGrid] = useState([{id: 1, step: ""}]);
|
|
|
+ const [resultsGrid, setResultsGrid] = useState([{id: 1, number: ""}]);
|
|
|
+ const [stepsGrid, setStepsGrid] = useState([{id: 1, step: ""}]);
|
|
|
|
|
|
- const [showSteps, setShowSteps] = useState(true);
|
|
|
+ const [autoMoveComma, setAutoMoveComma] = useState(false);
|
|
|
+ const [allowStartOver, setAllowStartOver] = useState(true);
|
|
|
|
|
|
|
|
|
let calculationInput = useRef(null);
|
|
@@ -26,7 +27,13 @@ function Subtraction() {
|
|
|
if(calculationInput.current && calculationInput.current.value === ""){
|
|
|
calculationInput.current.focus();
|
|
|
}
|
|
|
- },[])
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ useEffect(()=>{
|
|
|
+ if(!autoMoveComma){
|
|
|
+ addNumbersToTable([]);
|
|
|
+ }
|
|
|
+ }, [numbers]);
|
|
|
|
|
|
const handleInput = (e) => {
|
|
|
setInput(e.target.value);
|
|
@@ -56,15 +63,15 @@ function Subtraction() {
|
|
|
let noCarry = carryArrCopy[0] === "" || carryArrCopy[0] === "0" || carryArrCopy[0] === undefined;
|
|
|
if((nosLeft === 1 && noCarry) ||
|
|
|
|
|
|
- (nosLeft !== nosLeft && realResult.toString().length === 1 && noCarry)){
|
|
|
+ (isNaN(nosLeft) && realResult.toString().length === 1 && noCarry)){
|
|
|
nosLeft = 0;
|
|
|
handleResChange(" ")
|
|
|
}
|
|
|
|
|
|
if(nosLeft === 0){
|
|
|
- if(showSteps){
|
|
|
+ if(allowStartOver){
|
|
|
showIdmtResults(carryArrCopy);
|
|
|
- addButtonsToImdtSteps();
|
|
|
+ addButtonsToImdtSteps(carryArrCopy);
|
|
|
}else{
|
|
|
finishCalculation();
|
|
|
}
|
|
@@ -90,11 +97,73 @@ function Subtraction() {
|
|
|
setRealResult(realRes);
|
|
|
console.log("real result:", realRes);
|
|
|
|
|
|
- let [numbersArr, commaIdx] = numbersToArrOfArr(numbers);
|
|
|
- setNumbers(numbersArr);
|
|
|
- setCommaIdx(commaIdx);
|
|
|
+ let numbersArr, commaIdx;
|
|
|
+ if(!autoMoveComma){
|
|
|
+ numbersArr = numbers.map(x => x.toString().split(""));
|
|
|
+ setNumbers(numbersArr);
|
|
|
+ addNumbersToTable([]);
|
|
|
+ startCommaMove();
|
|
|
+
|
|
|
+ }else{
|
|
|
+ commaIsSet = true;
|
|
|
+ [numbersArr, commaIdx] = numbersToArrOfArr(numbers);
|
|
|
+ setNumbers(numbersArr);
|
|
|
+ setCommaIdx(commaIdx);
|
|
|
+ addNumbersToTable(numbersArr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- setNumbersGrid(addNumbersToGrid(numbersArr, "-"));
|
|
|
+ const handleKeyMoveComma = (e) => {
|
|
|
+ let pressedKey = e.keyCode;
|
|
|
+ let tdId = e.target.id;
|
|
|
+ let noId = tdId.split("numbersTd")[1];
|
|
|
+ let numbersCopy = [...numbers];
|
|
|
+
|
|
|
+ if(pressedKey === 37){
|
|
|
+ numbersCopy[noId].push(" ");
|
|
|
+ setNumbers(numbersCopy);
|
|
|
+ setTimeout(() => {
|
|
|
+ document.getElementById(tdId).focus();
|
|
|
+ }, 100);
|
|
|
+
|
|
|
+ }else if(pressedKey === 39 &&
|
|
|
+ numbersCopy[noId][numbersCopy[noId].length-1] === " "){
|
|
|
+ numbersCopy[noId].pop();
|
|
|
+ setNumbers(numbersCopy);
|
|
|
+ setTimeout(() => {
|
|
|
+ document.getElementById(tdId).focus();
|
|
|
+ }, 100);
|
|
|
+
|
|
|
+ }else if(pressedKey === 38){
|
|
|
+ let nextFocusId = Math.max(noId-1, 0);
|
|
|
+ document.getElementById("numbersTd" + nextFocusId).focus();
|
|
|
+
|
|
|
+ }else if(pressedKey === 40){
|
|
|
+ let nextFocusId = Math.min(noId+1, numbers.length-1);
|
|
|
+ document.getElementById("numbersTd" + nextFocusId).focus();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const addNumbersToTable = (numbersArr) => {
|
|
|
+ let nos;
|
|
|
+ if(numbersArr.length > 0){
|
|
|
+ nos = numbersArr;
|
|
|
+ }else{
|
|
|
+ nos = numbers;
|
|
|
+ }
|
|
|
+ let table = document.getElementById("numbersGrid");
|
|
|
+ table.innerHTML = "";
|
|
|
+ for(let noIdx in nos){
|
|
|
+ let row = table.insertRow();
|
|
|
+ let cell = row.insertCell();
|
|
|
+ let cellText = nos[noIdx].join("");
|
|
|
+ if(noIdx == nos.length-1){
|
|
|
+ cellText = "- " + cellText;
|
|
|
+ }
|
|
|
+ cell.innerHTML = cellText;
|
|
|
+ cell.tabIndex = "0";
|
|
|
+ cell.id = "numbersTd" + noIdx;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -107,12 +176,17 @@ function Subtraction() {
|
|
|
let firstDigit = 0;
|
|
|
let digit = 0;
|
|
|
|
|
|
- if(typeof numbers === "object" && imdtResIdx !== -1){
|
|
|
+ if(commaIsSet && typeof numbers === "object" && imdtResIdx !== -1){
|
|
|
+ document.getElementById("commaWarning").innerHTML = "";
|
|
|
+
|
|
|
|
|
|
if(typeof imdtResIdx === "undefined" || imdtResIdx === null){
|
|
|
noOfDigits = Math.min(...numbers.map(n => n.length));
|
|
|
imdtResIdx = noOfDigits - 1;
|
|
|
}
|
|
|
+ if(typeof nosLeft === "undefined" ){
|
|
|
+ nosLeft = imdtResIdx + 1;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
if(imdtResIdx === commaIdx){
|
|
@@ -123,17 +197,13 @@ function Subtraction() {
|
|
|
nosLeft = nosLeft - 1;
|
|
|
}
|
|
|
|
|
|
- if(numbers.length>2){
|
|
|
- labelText = "("
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
- for (let n in numbers){
|
|
|
+ for(let n in numbers){
|
|
|
if(parseInt(n)===0){
|
|
|
firstDigit = numbers[parseInt(n)][imdtResIdx];
|
|
|
}else{
|
|
|
|
|
|
- if(nosLeft === 1 && carryArr[0] === "0"){
|
|
|
+ if(nosLeft === 1 && (carryArr[0] === "0" || carryArr[0] === "")){
|
|
|
nosLeft = 0;
|
|
|
imdtResIdx = -1;
|
|
|
return <></>
|
|
@@ -149,9 +219,6 @@ function Subtraction() {
|
|
|
if(carryArr[0] !== undefined && carryArr[0] > 0){
|
|
|
labelText += " + " + carryArr[0].toString();
|
|
|
}
|
|
|
- if(numbers.length>2){
|
|
|
- labelText += ")";
|
|
|
- }
|
|
|
}else{
|
|
|
labelText += " + ";
|
|
|
}
|
|
@@ -194,7 +261,7 @@ function Subtraction() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- const addButtonsToImdtSteps = () => {
|
|
|
+ const addButtonsToImdtSteps = (carryArrCopy) => {
|
|
|
|
|
|
setTimeout(() => {
|
|
|
let table = document.getElementById("idmtResultSteps").getElementsByTagName("table")[0]
|
|
@@ -207,7 +274,7 @@ function Subtraction() {
|
|
|
btn.innerHTML = "hier starten";
|
|
|
btn.classList = "btn btn-secondary btn-sm";
|
|
|
btn.id = "btn-" + trIdx;
|
|
|
- btn.addEventListener("click", () => startOver(trIdx));
|
|
|
+ btn.addEventListener("click", () => startOver(trIdx, carryArrCopy));
|
|
|
td.appendChild(btn);
|
|
|
}
|
|
|
}
|
|
@@ -236,7 +303,7 @@ function Subtraction() {
|
|
|
|
|
|
let realDigitRes = 0;
|
|
|
let firstDigit = 0;
|
|
|
- for (let j=0; j<numbers.length; j++) {
|
|
|
+ for(let j=0; j<numbers.length; j++) {
|
|
|
if(parseInt(j)===0){
|
|
|
firstDigit = numbers[parseInt(j)][idxNumbers];
|
|
|
if(firstDigit==="" | firstDigit===" "){
|
|
@@ -324,11 +391,10 @@ function Subtraction() {
|
|
|
document.getElementById("idmtResultSteps").focus();
|
|
|
}
|
|
|
|
|
|
- const startOver = (idx) => {
|
|
|
+ const startOver = (idx, carryArrCopy) => {
|
|
|
let invertedIdx = numbers[0].length - idx - 1;
|
|
|
imdtResIdx = invertedIdx;
|
|
|
|
|
|
- let carryArrCopy = [...carryArr];
|
|
|
let slicer = invertedIdx;
|
|
|
if(resArr[0] !== " " && !resArr.includes(".")){
|
|
|
slicer += 1;
|
|
@@ -388,6 +454,76 @@ function Subtraction() {
|
|
|
document.getElementById("finishCalculation").focus();
|
|
|
}
|
|
|
|
|
|
+ const startCommaMove = () => {
|
|
|
+ document.getElementById("commaSubmit").style.display = "inline-block";
|
|
|
+ setTimeout(() => {
|
|
|
+ document.getElementById("numbersTd0").focus();
|
|
|
+ }, 300)
|
|
|
+ }
|
|
|
+
|
|
|
+ const submitCommaMove = () => {
|
|
|
+ let numbersCopy = [...numbers];
|
|
|
+ let commaPositions = [];
|
|
|
+
|
|
|
+
|
|
|
+ for(let noIdx in numbersCopy){
|
|
|
+ while(numbersCopy[noIdx][0] === " "){
|
|
|
+ numbersCopy[noIdx].shift();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ let numbersLen = Math.max(...numbers.map(n => n.length));
|
|
|
+ for(let noIdx in numbersCopy){
|
|
|
+ while(numbersCopy[noIdx].length <= numbersLen){
|
|
|
+ numbersCopy[noIdx].unshift(" ");
|
|
|
+ }
|
|
|
+ commaPositions.push(numbersCopy[noIdx].indexOf("."));
|
|
|
+ }
|
|
|
+ setNumbers(numbersCopy);
|
|
|
+
|
|
|
+ let commaCorrect = false;
|
|
|
+ commaPositions = [...new Set(commaPositions.filter(c => c >= 0))];
|
|
|
+
|
|
|
+
|
|
|
+ if(commaPositions.length === 0){
|
|
|
+ setCommaIdx(-1);
|
|
|
+
|
|
|
+ commaCorrect = !numbersCopy.map(n => n[numbersLen] !== " ").includes(false)
|
|
|
+
|
|
|
+
|
|
|
+ }else if(commaPositions.length === 1){
|
|
|
+ setCommaIdx(commaPositions[0]);
|
|
|
+ commaCorrect = true;
|
|
|
+
|
|
|
+
|
|
|
+ for(let no of numbersCopy){
|
|
|
+ if(!no.includes(".")){
|
|
|
+
|
|
|
+ if(no[commaPositions[0]-1] === " "){
|
|
|
+ commaCorrect = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ for(let digit=commaPositions[0]; digit<=numbersLen; digit++){
|
|
|
+ if(no[digit] !== " "){
|
|
|
+ commaCorrect = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(commaCorrect){
|
|
|
+ document.getElementById("commaSubmit").style.display = "none";
|
|
|
+ commaIsSet = true;
|
|
|
+ }else{
|
|
|
+ let paragraph = document.getElementById("commaWarning");
|
|
|
+ paragraph.innerHTML = "das ist leider falsch, versuche es noch einmal:";
|
|
|
+ paragraph.tabIndex = "0";
|
|
|
+ paragraph.focus();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return (
|
|
|
<main>
|
|
|
|
|
@@ -408,17 +544,19 @@ function Subtraction() {
|
|
|
</form>
|
|
|
|
|
|
<div id="finishCalculation"></div>
|
|
|
+ <div id="commaWarning"></div>
|
|
|
|
|
|
<div id="overview">
|
|
|
- <DataGrid
|
|
|
- dataSource={numbersGrid}
|
|
|
- keyExpr="id"
|
|
|
- focusedRowEnabled={true}
|
|
|
- showBorders={true}
|
|
|
- showColumnHeaders={false}
|
|
|
+ <table
|
|
|
+ id="numbersGrid"
|
|
|
+ onKeyDown={handleKeyMoveComma}
|
|
|
>
|
|
|
- <Column dataField="number" />
|
|
|
- </DataGrid>
|
|
|
+ </table>
|
|
|
+
|
|
|
+ <input type="submit"
|
|
|
+ value="Komma bestätigen"
|
|
|
+ id="commaSubmit"
|
|
|
+ onClick={() => submitCommaMove()}/>
|
|
|
|
|
|
<hr></hr>
|
|
|
|