Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10进制转2进制的尾递归版本 #29

Open
zwhu opened this issue Nov 25, 2016 · 0 comments
Open

10进制转2进制的尾递归版本 #29

zwhu opened this issue Nov 25, 2016 · 0 comments

Comments

@zwhu
Copy link
Owner

zwhu commented Nov 25, 2016

这是一篇水文,老板给面试同学出的笔试题,顺手练一下

function toBinary(n) {
  function toBinaryIter(n, s) {
     if(n === 0) {
       return s || '0'
     }
     
     var a = n / 2
     , b = n % 2

     return toBinaryIter(a|0, b+s)
  }

  return toBinaryIter(n, '')
}


toBinary(0) === (0).toString(2)

toBinary(1) === (1).toString(2)

toBinary(3) === (3).toString(2)

toBinary(10) === (10).toString(2)

toBinary(101) === (101).toString(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant