소스 검색

moved functions to helpers file

Bernadette Elena Hammerle 4 년 전
부모
커밋
74785b3211
2개의 변경된 파일68개의 추가작업 그리고 61개의 파일을 삭제
  1. 8 61
      src/Addition.js
  2. 60 0
      src/helpers.js

+ 8 - 61
src/Addition.js

@@ -1,6 +1,7 @@
 import React, { useState, useEffect, useRef } from "react";
 import DataGrid, {Column} from "devextreme-react/data-grid";
-import {Alert} from "./AlertDialog.js"
+import {Alert} from "./AlertDialog.js";
+import {addNumbersToGrid, numbersToArrOfArr} from "./helpers.js";
 import "./App.css";
 
 let imdtRes;
@@ -64,69 +65,16 @@ function Addition() {
     e.preventDefault(); // avoid page reload
   }
 
-  const afterCommaLen = (number) => {
-    let noStrList = number.toString().split(".");
-    if (noStrList.length > 1){
-      return noStrList[1].length
-    }
-    return 0
-  }
-
-  function numbersToArrOfArr(numbers) {
-    let befComma = Math.max(...numbers).toString().split(".")[0].length;
-    let afterComma = Math.max(...numbers.map(x => afterCommaLen(x)));
-    let withComma = Math.max(...numbers.map(x => x.toString().indexOf(".")));
-
-    let arrLength;
-    if(withComma < 0){ // no comma found
-      arrLength = befComma;
-    }else{
-      arrLength = befComma + afterComma + 1; // add comma and after comma len
-    }
-
-    let numbersArr = numbers.map(x => x.toString().split(""));
-    for (let numArr of numbersArr){
-      let commaIdx = numArr.indexOf(".");
-
-      // without comma, add before
-      while(commaIdx===-1 && numArr.length < befComma){
-        numArr.unshift("&nbsp;"); // add " " before comma
-      }
-      // without comma, add after
-      while(commaIdx===-1 && numArr.length < arrLength){
-        numArr.push("&nbsp;"); // add " " after comma
-      }
-      // with comma, add before
-      while(commaIdx>-1 && commaIdx!==befComma){
-        numArr.unshift("&nbsp;"); // add " " before comma
-        commaIdx = numArr.indexOf(".");
-      }
-      // with comma, add after
-      while(numArr.length < arrLength){
-        numArr.push("&nbsp;"); // add " " after comma
-      }
-      numArr.unshift("&nbsp;"); // for carry
-    }
-    setNumbers(numbersArr);
-
-    const nrGrid = []
-    for (let noIdx in numbersArr){
-      let nr = numbersArr[noIdx].join("").replace(/&nbsp;/g, " ");
-      if (noIdx == numbersArr.length-1){
-        nr = "+ " + nr;
-      }
-      nrGrid.push({id: parseInt(noIdx)+3, number: nr});
-    }
-    setNumbersGrid(nrGrid);
-    setCommaIdx(befComma+1);
-    // TODO: assert all have same length and comma at same index?
-  }
-
   const startCalculation = () => {
     if (input.includes("+")){
       let numbers = input.split("+").map(x => parseFloat(x.replace(",",".")));
       setRealResult(numbers.reduce((x,y) => x+y, 0));
-      numbersToArrOfArr(numbers);
+
+      let [numbersArr, commaIdx] = numbersToArrOfArr(numbers);
+      setNumbers(numbersArr);
+      setCommaIdx(commaIdx);
+
+      setNumbersGrid(addNumbersToGrid(numbersArr));
     }
   }
 
@@ -293,7 +241,6 @@ function Addition() {
     btnSubmit.innerHTML = "Ergebnis abgeben";
     btnSubmit.addEventListener("click", finishCalculation);
     document.getElementById("idmtResultSteps").appendChild(btnSubmit);
-
   }
 
   const startOver = (idx) => {

+ 60 - 0
src/helpers.js

@@ -0,0 +1,60 @@
+ export const addNumbersToGrid = (numbersArr) => {
+  const nrGrid = []
+  for (let noIdx in numbersArr){
+    let nr = numbersArr[noIdx].join("").replace(/&nbsp;/g, " ");
+    if (noIdx == numbersArr.length-1){
+      nr = "+ " + nr;
+    }
+    nrGrid.push({id: parseInt(noIdx)+3, number: nr});
+  }
+  return nrGrid;
+}
+
+
+const afterCommaLen = (number) => {
+  let noStrList = number.toString().split(".");
+  if (noStrList.length > 1){
+    return noStrList[1].length
+  }
+  return 0
+}
+
+
+export function numbersToArrOfArr(numbers) {
+  let befComma = Math.max(...numbers).toString().split(".")[0].length;
+  let afterComma = Math.max(...numbers.map(x => afterCommaLen(x)));
+  let withComma = Math.max(...numbers.map(x => x.toString().indexOf(".")));
+
+  let arrLength;
+  if(withComma < 0){ // no comma found
+    arrLength = befComma;
+  }else{
+    arrLength = befComma + afterComma + 1; // add comma and after comma len
+  }
+
+  let numbersArr = numbers.map(x => x.toString().split(""));
+  for (let numArr of numbersArr){
+    let commaIdx = numArr.indexOf(".");
+
+    // without comma, add before
+    while(commaIdx===-1 && numArr.length < befComma){
+      numArr.unshift("&nbsp;"); // add " " before comma
+    }
+    // without comma, add after
+    while(commaIdx===-1 && numArr.length < arrLength){
+      numArr.push("&nbsp;"); // add " " after comma
+    }
+    // with comma, add before
+    while(commaIdx>-1 && commaIdx!==befComma){
+      numArr.unshift("&nbsp;"); // add " " before comma
+      commaIdx = numArr.indexOf(".");
+    }
+    // with comma, add after
+    while(numArr.length < arrLength){
+      numArr.push("&nbsp;"); // add " " after comma
+    }
+    numArr.unshift("&nbsp;"); // for carry
+  }
+  return [numbersArr, befComma+1];
+  // TODO: assert all have same length and comma at same index?
+}