123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- export const addNumbersToGrid = (numbersArr, sign) => {
- const nrGrid = []
- for (let noIdx in numbersArr){
- let nr = numbersArr[noIdx].join("").replace(/ /g, " ");
- if (parseInt(noIdx) === numbersArr.length-1){
- nr = sign + " " + 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(" "); // add " " before comma
- }
- // without comma, add after
- while(commaIdx===-1 && numArr.length < arrLength){
- numArr.push(" "); // add " " after comma
- }
- // with comma, add before
- while(commaIdx>-1 && commaIdx!==befComma){
- numArr.unshift(" "); // add " " before comma
- commaIdx = numArr.indexOf(".");
- }
- // with comma, add after
- while(numArr.length < arrLength){
- numArr.push(" "); // add " " after comma
- }
- numArr.unshift(" "); // for carry
- }
- return [numbersArr, befComma+1];
- // TODO: assert all have same length and comma at same index?
- }
|