// Separate Numbers with Commas in JavaScript


// I worked on this challenge with: .

// Pseudocode

//Input:  integer
//Output: integer - with commas - as a string
//Steps:
/*
1. Make integer into String with each digit as it's own element
2. Reverse the number
3. Every 3 digits, add a comma
4. Reverse the number
5. RETURN a string
*/

// Initial Solution
/*
function separateComma(integer) {
  var string = integer.toString();
  //return string.split('').reverse().join('');
  var string_comma = "";
  for (var counter = string.length; counter > 0; counter -= 3) {
    var sub = string.substring(counter, (counter -3))
    string_comma = sub + "," + string_comma;
    
  }
  return(string_comma.substring(0, string_comma.length-1));
};
*/
//console.log(separateComma(100000));

// Refactored Solution

function separateComma(integer) {
  var string = integer.toString();
  var string_comma = "";
  for (var counter = string.length; counter > 0; counter -= 3) {
    var sub = string.substring(counter, (counter -3));
    if (counter === string.length) {
      string_comma = sub.concat(string_comma);
      continue;
    }
    string_comma = sub.concat(",", string_comma);
    
  }
  return string_comma;
};

console.log(separateComma(100));


// Your Own Tests (OPTIONAL)
function assert(test, message) {
  if (!test) {
    throw "Error: " + message;
  }
  return true;
}

assert(
  (typeof separateComma(10000) === 'string'),
  "It should output a String."
)
assert(
  (separateComma(100) === "100"),
  "There should be no commas."
)
assert(
  (separateComma(1000) === "1,000"),
  "There should be one comma between the thousands and hundreds place."
)
assert(
  (separateComma(10000) == "10,000"),
  "There should be one comma between the thousands and hundreds place."
)
assert(
  (separateComma(100000) === "100,000"),
  "There should be one comma between the thousands and hundreds place."
)
assert(
  (separateComma(1000000) === "1,000,000"),
  "There should be two commas. One between the thousands and hundreds place. The other between the millions and hundred thousands place."
)



// Reflection

// Feedback
/*It was great working you again. I thought we had good communication. You were great at 
suggesting ideas for refactoring and I'm glad you wanted to do the bonus and make the tests. 
I'd gladly pair with you again.*/

@@ -77,16 +77,27 @@
 e with: 
+Max Iniguez
 .%0A%0A// Ps
@@ -107,17 +107,18 @@
 ocode%0A%0A/
-/
+*%0A
 Input:  
@@ -125,18 +125,16 @@
 integer%0A
-//
 Output: 
@@ -173,19 +173,15 @@
 ing%0A
-//
 Steps:%0A
-/*
 %0A1. 
@@ -335,18 +335,16 @@
  string%0A
-*/
 %0A%0A// Ini
@@ -357,18 +357,16 @@
 olution%0A
-/*
 %0Afunctio
@@ -740,13 +740,9 @@
 %0A%7D;%0A
-*/%0A//
+%0A
 cons
@@ -772,16 +772,19 @@
 00000));
+%0A*/
 %0A%0A// Ref
@@ -2111,233 +2111,1342 @@
 n%0A%0A/
-/ Feedback%0A/*It was great working you again. I though
+*%0A%0AWhat was it like to approach the problem from the perspective of JavaScript? Did you approach the problem differently?%0A%0AIt was interesting because we didn't have all the built in methods (and knowledge) tha
 t we
- 
+%0A
 had 
-good communication. You were great at %0Asugge
+when approaching the problem in Ruby. We had to try the simplest steps possible since%0Awe weren't sure how the behaviors of strings and arrays differed from Ruby in JavaScript.%0A%0A%0AWhat did you learn about iterating over arrays in JavaScript?%0A%0AI learned that we could iterate over strings, instead of arrays in JavaScript. Which meant%0Athat the index goes from 1 to string.length instead of from 0 to (
 st
+r
 ing
- ideas for refactoring and I'm glad you wanted to do the bonus and make the tests. %0AI'd gladly pair with you again.
+.length-1).%0AAdditionally for a string there was no built in reverse function. So we needed to manually%0Aiterate from back to front instead of using a built in method to reverse the order.%0A%0A%0AWhat was different about solving this problem in JavaScript?%0A%0AThere were less built in methods to use. But we could modify the string much more easily.%0AWe didn't /have/ to convert the integer into an array made up of single digits. It was %0Apossible to just keep the integer in string form and modify the string.%0A%0A%0AWhat built-in methods did you find to incorporate in your refactored solution?%0A%0AWe used a concat method to put together the strings and substrings. We also used toString%0Ato convert the integer into a string.%0A%0A%0A%0A
 */
-%0A

Write reflection