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. python学习第五天流程控制分支if和循环while

    所有的逻辑结构围绕分支和循环进行,比如登陆注册,支付成功与否等等,下面讲述分支if用法和while用法 if age>30: print("www.96net.com.cn" ...

  2. 攻防世界--simple-unpack

    下载链接:https://adworld.xctf.org.cn/media/task/attachments/b7cf4629544f4e759d690100c3f96caa 1.准备 获取到信息: ...

  3. 卷积神经网络CNN原理以及TensorFlow实现

    在知乎上看到一段介绍卷积神经网络的文章,感觉讲的特别直观明了,我整理了一下.首先介绍原理部分. [透析] 卷积神经网络CNN究竟是怎样一步一步工作的? 通过一个图像分类问题介绍卷积神经网络是如何工作的 ...

  4. 解决:Module not found: node_modules\sass-loader\package.json (directory description file)

    npm uninstall node-sass npm install node-sass@latest

  5. 【彩彩只能变身队(第七组)】Beta版本

    本篇博客包括前期博文汇总.任务墙.团队管理细节与交流细节.代码管理.Beta阶段冲刺.团队总结.用户使用报告.Postmortem报告. 服务器网址:http://47.106.227.154/ 彩彩 ...

  6. django 常用 import

    from django.shortcuts import HttpResponse, render, redirect def yimi(request): #直接返回页面内容 return Http ...

  7. 让docker容器使用主机系统时间(挂入/etc/localtime)

    -v挂入这个文件就可以了: -v /etc/localtime:/etc/localtime:ro

  8. 牛客小白月赛16 H小阳的贝壳 (线段树+差分数组)

    链接:https://ac.nowcoder.com/acm/contest/949/H来源:牛客网 题目描述 小阳手中一共有 n 个贝壳,每个贝壳都有颜色,且初始第 i 个贝壳的颜色为 colico ...

  9. Ehcahe独立使用

    <?xml version="1.0" encoding="utf-8"?><ehcache xmlns:xsi="http://w ...

  10. day17 python re模块 简易爬虫

    day17 python   一.re模块     1.re模块的基础方法         查找findall() import re #re.findall(pattern,string,flags ...