format price

by John Allan

JavaScript

const formatPrice = price => {
	const p = typeof price === 'number' ? price.toString() : price;
  const pArray = p.split('').reverse();
  const fArray = [];

  pArray.forEach((v, i) => {
  	fArray.push(v);
    const c = i + 1;
    if (c < pArray.length && c % 3 === 0) {
    	fArray.push(',');
    }
  });
	return fArray.reverse().join('');
}

console.log(formatPrice(123345))