-3

Trying to find the min for this stack; however, whenever I run this in JSFiddle nothing prints out... anyone explain to me why? here's the code:

function min_stack() {
var min = 0; 
this.elements = [];

this.push = function(element) {
    this.elements.push(element);
}

this.pop = function() {
    return this.elements.pop();
}

this.min = function() {
    min = this.elements[0]; 
    if (this.elements.length > 0) {
        for(int i = 0; i < this.elements.length; i++) {
            if (min > this.elements[i]) {
                min = this.elements[i]; 
            }
        }
    }
    return min; 
   }    

}

var myStack = new min_stack();
myStack.push(5);
myStack.push(4);
myStack.push(3);
print("[" + myStack.elements + "]");
print("min:" + myStack.min());
myStack.pop();
print("[" + myStack.elements + "]");
print("min:" + myStack.min());
myStack.pop();
print("[" + myStack.elements + "]");
print("min:" + myStack.min());
girlrockingguna
  • 291
  • 1
  • 3
  • 13

2 Answers2

2

There is a syntax error in your for which shows up immediately in browser console

Change:

for(int i = 0; i < this.elements.length; i++) { 

TO

for(var i = 0; i < this.elements.length; i++) { 

DEMO: http://jsfiddle.net/y7wET/

ALso as pointed out in comments I doubt you want to use print

charlietfl
  • 170,828
  • 13
  • 121
  • 150
1

int i = 0; is not valid JavaScript. JavaScript does not allow you to specify the type of a variable when declaring it; instead, use var i = 0;.

Also, because "window" is the global object, in the context of the Web page, print() is equivalent to window.print(), which prints the page to your printer.

For debugging purposes, you can pop up a message box using window.alert(); if that is too annoying, you could do something such as adding your output to a textarea element instead.

PleaseStand
  • 31,641
  • 6
  • 68
  • 95