소스 검색

fixed number array length and comma position

Bernadette Elena Hammerle 3 년 전
부모
커밋
33fca93c76
1개의 변경된 파일13개의 추가작업 그리고 1개의 파일을 삭제
  1. 13 1
      src/App.js

+ 13 - 1
src/App.js

@@ -35,9 +35,21 @@ function App() {
 
     let numbersArr = numbers.map(x => x.toString().split(""))
     for (let numArr of numbersArr){
-      while (numArr.indexOf(".")>-1 && numArr.indexOf(".")!==befComma){
+      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
       }
+      // with comma, add after
       while (numArr.length < arrLength){
         numArr.push("&nbsp;"); // add " " after comma
       }