你的位置:首页 > 软件开发 > Java > Stack of js

Stack of js

发布时间:2017-11-07 09:00:07
function Stack() { this.dataStore = []; //存储栈元素}Stack.prototype = { constructor: Stack, push: function(element) { this.dataStore.push(eleme ...
function Stack() { this.dataStore = []; //存储栈元素}Stack.prototype = { constructor: Stack, push: function(element) {  this.dataStore.push(element) }, pop: function() {  return this.dataStore.pop() }, peek: function() {  return this.dataStore[this.dataStore.length - 1]; }, length: function() {  return this.dataStore.length; }, clear: function() {  this.dataStore.length = 0; }};//转换进制function mulBase(num, base) { var s = new Stack(); do {  s.push(num % base);  num = Math.floor(num /= base); } while (num > 0); var converted = ""; while (s.length() > 0) {  converted += s.pop(); } return converted;}var num = 125;var base = 8;//125转8进制var newNum = mulBase(num, base);console.log(num + " converted to base " + base + " is " + newNum);//验证回文串function isPalindrome(word) { var s = new Stack(); for (var i = 0; i < word.length; ++i) {  s.push(word[i]); } var rword = ""; while (s.length() > 0) {  rword += s.pop(); } if (word == rword) {  return true; } else {  return false; }}var word = "ollo";if (isPalindrome(word)) { console.log(word + " is a palindrome.");} else { console.log(word + " is not a palindrome.");}

  

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:Stack of js

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。