Browse Source

fixed number array length and comma position

Bernadette Elena Hammerle 3 years ago
parent
commit
33fca93c76
1 changed files with 13 additions and 1 deletions
  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
       }