请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

作业

class Stack {
  constructor(stack) {
    this.stack = stack || [];
  }

  push(value) {
    this.stack.push(value);
  }

  pop() {
    return this.stack.pop();
  }

  peek() {
    return this.stack[this.stack.length - 1];
  }

  getLength() {
    return this.stack.length
  }
}

function tenToTwo(num) {
  const stack = new Stack();
  let str = ''

  while (num > 0) {
    stack.push(num % 2);
    num = Math.floor(num / 2);
  }

  const len = stack.getLength();
  for (let i = 0; i < len; i++) {
    str += stack.pop();
  }

  return parseInt(str)
}

console.log(tenToTwo(100))

正在回答 回答被采纳积分+3

1回答

lewis 2020-10-10 16:18:32

good

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信