Stack Test

by Sam Fereday

JavaScript

var stack = [];
var stackLen = stack.length;
var maxSize = 10;

function getTop()
{
		return stack[stack.length - 1];
}

function pushStack(n)
{
		if(stackLen - 1 > maxSize)
    	return;
    
    stack.push(n);
    stackLen = stack.length;
    
}

function popStack()
{
		if(stackLen === 0)
    	return;
    
    console.log("Splice?", stack);
    stack.splice(stackLen - 1, 1);
    stackLen = stack.length;

}

function getStackAt(n)
{
		return stack[n];
}

function isEmpty()
{
	 return stack.length > 0 ? true:false;
}

function cutStackAt(idx)
{
		// So the stack might fall over just doing this. But hey well.
		stack.splice(idx, 1);
    
}

function sortStack()
{
   //...
}

pushStack("A");
pushStack("B");
pushStack("C");
pushStack("D");

console.log(stack);

// Removes D
popStack();

// Removes B
cutStackAt(1);

// Leaves A and C
console.log(stack);

// Removes C (bug: Popping twice removes 'c', but once does not...)
popStack();

// Leaves A
console.log(stack);