[Algorithm] Convert a number from decimal to binary

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

 

posted @ 2019-05-10 21:50  Zhentiw  阅读(233)  评论(0)    收藏  举报