2

The first thing i want to ask is if there is a more efficient way of writing this programm of mine. The second question is why on earth after a specific number of characters the programm does not function well and prints zeros.

function toCurrency(price) {
  var currencyToString = price.toString();
  var finalPrice = "";
  var counterComma = 0;
  for (var i = 0; i < currencyToString.length; i++) {
    counterComma++;
    finalPrice += currencyToString[i];
    if(counterComma == 3){
        counterComma = 0;
      finalPrice += ",";
    }
  }
  return finalPrice;
}

console.log(toCurrency(123456253635423197874));

https://jsfiddle.net/DimitriXd4/68epen4n/

Dimitris Xydas
  • 233
  • 2
  • 5
  • 9

0 Answers0