'use strict'

let Stack  = function(max){
  if(typeof max != 'number' || max == NaN){
    throw new Error(max + ' should be a number');
  }

  var Store = [],
      top = 0,
      empty = function() { return top === 0; },
      full = function(){ return top === max; },
      push =  function(ele){
        if(!this.full()){
          Store[top++] = ele;
        }else{
          throw new Error('Stack is overflow');
        }
      },
      pop = function(){
        if(!empty()){
          Store.splice(top-- - 1,  1);
        }else{
          throw new Error('Stack is empty, there is no element to be popped');
        }
      },
      clear = function(){
        Store = [];
        top = 0;
      },
      peek = function(){
        if(!empty()){
          return Store[top -1 ];
        }
        throw new Error('Stack is empty, there is no element to be peeked');
      },
      toString =  function(){
        let i = 0;
        if(!empty()){
          for(i; i < top; i++){
            console.log(Store[i]);
          }
        }
      };

  return {
    empty: empty,
    full: full,
    push: push,
    pop: pop,
    clear: clear,
    peek: peek,
    toString: toString
  };
};