SOURCE

function decimalToBinaryIterative(decimal) {
    let binary = '';
    let remainder;

    do {
        remainder = decimal % 2;
        binary = remainder.toString() + binary;
        decimal = Math.floor(decimal / 2);
    } while (decimal > 0);

    return binary;
}
//使用内置函数
function decimalToBinaryBuiltIn(decimal) {
    return decimal.toString(2);
}

// 示例
console.log(decimalToBinaryIterative(10)); // 输出 "1010"
console 命令行工具 X clear

                    
>
console