JavaScript算法试题系列-08 发表于 2017-06-15 | 更新于 2017-06-15 | 分类于 学习笔记 , JavaScript | | 共 100 字 | 阅读 次 问题描述: 123// 递归二进制转换通过某个递归函数将输入的数字转化为二进制字符串:decimalToBinary(3); // 11decimalToBinary(8); // 1000decimalToBinary(1000); // 1111101000 解答: 123456789101112131415function decimalToBinary(digit) { if(digit >= 1) { // If digit is not divisible by 2 then recursively return proceeding // binary of the digit minus 1, 1 is added for the leftover 1 digit if (digit % 2) { return decimalToBinary((digit - 1) / 2) + 1; } else { // Recursively return proceeding binary digits return decimalToBinary(digit / 2) + 0; } } else { // Exit condition return ''; }} ------ 本文结束 ------ 版权声明 加州提子面包 by MiG is licensed under a Creative Commons BY-NC-SA 4.0 International License. 由MiG创作并维护的加州提子面包博客采用「创作共用保留署名-非商业性使用-相同方式共享」4.0国际许可证. 本文首发于加州提子面包 博客( https://gihcctpd.github.io ),版权所有,侵权必究.