function Stack() {
var items = [];
this.push = function(item) {
items.push(item)
}
this.pop = function() {
return items.pop()
}
this.peek = function() {
return items[items.length - 1]
}
this.isEmpty = function() {
return items.length == 0
}
this.size = function() {
return items.length
}
this.clear = function() {
items = []
}
this.printf = function() {
console.log(items.toString())
}
this.divideBy2 = function(decNumber) {
var remStack = new Stack(),
rem,
binaryString = '';
while (decNumber > 0) {
rem = Math.floor(decNumber % 2);
remStack.push(rem);
decNumber = Math.floor(decNumber / 2)
}
while (!remStack.isEmpty()) {
binaryString += remStack.pop().toString()
}
return binaryString
}
}
var stacks = new Stack();
console.log(stacks.isEmpty());
stacks.push(5);
stacks.push(4);
console.log(stacks.peek());
stacks.push(11);
console.log(stacks.size());
console.log(stacks.isEmpty());
stacks.push(15);
stacks.pop();
console.log(stacks.size());
stacks.printf();
console.log(stacks.divideBy2(33));

  

JavaScript Stack的更多相关文章

  1. [Algorithom] Stack Data Structure in JavaScript

    A stack is a collection of items that obeys the principle of "last in, first out". Like a ...

  2. JavaScript 查看stack trace

    How can I get a JavaScript stack trace when I throw an exception? Edit 2 (2017): In all modern brows ...

  3. JavaScript取子串方法slice,substr,substring对比表

    在程序语言中,字符串可以说是最常用的一种类型,而在程序中对字符串的操作也是十分频繁.当程序语言自带多种字符串操作的方法时,用该语言编程程序时就有很多的便利性,提高开发的效率.但是当方法过多,甚至目的相 ...

  4. (转) [it-ebooks]电子书列表

    [it-ebooks]电子书列表   [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...

  5. Cheatsheet: 2015 09.01 ~ 09.30

    Web A Guide to Vanilla Ajax Without jQuery Gulp for Beginners A Detailed Walkthrough of ASP.net MVC ...

  6. [Ramda] Lens in Depth

    In this post, we are going to see how to use Ramda Lens. For example, we have data: const {log} = re ...

  7. 摘录和再编:彻底弄懂JS执行机制

    网文: https://juejin.im/post/59e85eebf265da430d571f89 并发模型和事件循环:https://developer.mozilla.org/zh-CN/do ...

  8. [React] 05 - Route: connect with ExpressJS

    基础: 初步理解:Node.js Express 框架 参见:[Node.js] 08 - Web Server and REST API 进阶: Ref: 如何系统地学习 Express?[该网页有 ...

  9. Web 前端从入门菜鸟到实践老司机所需要的资料与指南合集

    http://web.jobbole.com/89188/ 2016 – 对于未来五年内Web发展的7个预测 2015 – 我的前端之路:从命令式到响应式,以及组件化与工程化的变革 怎么成为一名优秀的 ...

随机推荐

  1. [fw]Linux系统使用time计算命令执行的时间

    Linux系统使用time计算命令执行的时间 当测试一个程序或比较不同算法时,执行时间是非常重要的,一个好的算法应该是用时最短的.所有类UNIX系统都包含time命令,使用这个命令可以统计时间消耗.例 ...

  2. Codeforces - 1195E - OpenStreetMap - 单调队列

    https://codeforc.es/contest/1195/problem/E 一个能运行但是会T的版本,因为本质上还是\(O(nmab)\)的算法.每次\(O(ab)\)初始化矩阵中的可能有用 ...

  3. HDU 5441 Travel (离线dsu)

    <题目链接> 题目大意:$n$个点,$m$条边,每条边具有对应的权值,然后进行$k$次询问,每次询问给定一个值,所有权值小于等于这个的边所对应的点能够相连,问每次询问,这些能够相互到达的点 ...

  4. 对VS2019进行32位汇编环境配置

    1.库文件(很重要) 用我这一份就行:https://www.lanzous.com/i6364hg 2.VS依赖库 打开VS2019,选择桌面向导 配置项目时,选择新项目. 选择生成依赖项 选中ma ...

  5. 一、Google开发者工具功能页面截图

    一.利用Chrome开发者工具功能进行网页整页截图的方法. 打开你想截图的网页,然后按下 F12(macOS 是 option + command + i)调出开发者工具, 接着按「Ctrl + Sh ...

  6. Polish orthography

    Computer encoding[edit] There are several different systems for encoding the Polish alphabet for com ...

  7. Kotlin 的 Array 转 List

    Kotlin 的 Array 转 List array.toList() as List<T> 1 Kotlin 的 Array 转 ArrayList array.toList() as ...

  8. python socket基本连接功能实现

    socket是在应用层和传输层之间的一个抽象层,它把TCP/IP层复杂的操作抽象为几个简单的接口供应用层调用已实现进程在网络中通信 在应用中,要写两个程序来通信,一个是服务器端程序,一个是客户端程序 ...

  9. 用pycharm运行pytest

    安装pytest 1. 在pycharm中建项目,建文件,文件名字要以test_开头 2.在文件中插入pytest模块 import pytest #引用pytest模块 3.定义test函数,以及断 ...

  10. 【leetcode】399. Evaluate Division

    题目如下: Equations are given in the format A / B = k, whereA and B are variables represented as strings ...