let result = ''; let x; while (true) { if (x % 2 == 0) { result += '0'; } else { result += '1'; } x = Math.floor(x / 2); if ((x == 1) | (x == 0)) { result += String(x); } } console.log(result);