stack & queue
by Samar Pattanayak
JavaScript
var Stack = function() {
this._size = 0;
this._storage = {};
}
Stack.prototype.push = function(item) {
if (item) {
var pos = this._size++;
this._storage[pos] = item;
}
console.log(this._storage);
}
Stack.prototype.pop = function() {
var posR = --this._size;
delete this._storage[posR];
console.log(this._storage);
}
var sOb = new Stack();
//sOb.push(2);sOb.push(12);sOb.push(22);sOb.push(32);
//sOb.pop();
var Queue = function() {
this._storage = {};
this._oldIndex = 1;
this._newIndex = 1;
}
Queue.prototype.enqueue = function(item) {
if (item) {
var pos = this._newIndex++;
this._storage[pos] = item;
}
console.log(this._storage);
}
Queue.prototype.dequeue = function() {
var posR = this._oldIndex++;
delete this._storage[posR];
console.log(this._storage);
}
Queue.prototype.size = function() {
return parseInt(this._newIndex - this._oldIndex);
}
var qOb = new Queue();
//qOb.enqueue(11);qOb.enqueue(12);qOb.enqueue(13);qOb.enqueue(14);
//qOb.dequeue();
//console.log(qOb.size());
var arr = [2, 3]
for (let i of arr) {
console.log(arr[i])
}
console.log("&&&&&&&&&&&&&&&&&&&&")
var arr = [5, 5, 4, 3, 2];
//var arr=[5,5,5,5,5];
var d = 0;
var sumArr = [];
for (var i = 0; i < arr.length; i++) {
d = arr[i];
var check = [];
for (var j = i; j < arr.length; j++) {
if (j != arr.length - 1) {
check.push(arr[j + 1]);
}
if (i > 0 && j == i) {
for (var k = i; k > 0; --k) {
check.push(arr[k - 1])
}
}
}
//console.log(check)
var sum = check.reduce(function(a, b) {
return a + b
})
sumArr.push(sum);
//console.log(sumArr);
}
var max = sumArr.reduce(function(x, y) {
if (x > y) {
return x;
} else {
return y;
}
})
var min = sumArr.reduce(function(x, y) {
if (x < y) {
return x;
} else {
return y;
}
})
console.log(max, min)
var time = "01:40:22AM";
console.log(time.substring(time.length - 2));
if (time.substring(time.length - 2) == "PM") {
var...