const number = 180; let arr = []; let flag = true; getPrime(number); print(arr.join(' ')); function getPrime(num) { if (num === 1) { return null; } for (let i = 2; i <= num/i; i++) { if (Math.ceil(num/i) === num/i){ arr.push(i); getPrime(num/i); flag = false; break; } } if (flag) { arr.push(num); } }