码迷,mamicode.com
首页 > 其他好文 > 详细

[Algorithm] Convert a number from decimal to binary

时间:2019-05-10 21:56:52      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:algo   number   UNC   console   list   fun   cti   rem   lock   

125, how to conver to binary number?

技术图片

 

function DecimalToDinary (n) {
  let temp = n;
  let list = [];

  if (temp <= 0) {
    return ‘0000‘;
  }

  while (temp > 0) {
    let rem = temp % 2;
    list.push(rem);
    temp = parseInt(temp / 2, 10);
  }

  return list.reverse().join(‘‘);
}

console.log(DecimalToDinary(125)) // 1111101

 

[Algorithm] Convert a number from decimal to binary

标签:algo   number   UNC   console   list   fun   cti   rem   lock   

原文地址:https://www.cnblogs.com/Answer1215/p/10846926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!