peterschussheim
10/31/2016 - 1:15 PM

assessment-week1

assessment-week1

var reduceRight = function(array, callback, initial) {
  var length = array.length;
  if (arguments.length < 3) {
    if (length > 0) {
      var result = array[--length];
    }
  } else {
    var result = initial;
  }
  while (length > 0) {
    var index = --length;
    result = callback(array[index], result, index, array);
  }

  return result;
};
var makeHashTable = function() {
  var max = 4;
  return {
    _storage: [],

    hashFn: function(str, max) {
      var hash = 0;
      for (var i = 0; i < str.length; i++) {
        var letter = str[i];
        hash = (hash << 5) + letter.charCodeAt(0);
        hash = (hash & hash) % max;
      }
      return hash;
    },

    retrieve: function(key) {
      return this._storage[this.hashFn(key, max)];
    },

    insert: function(key, value) {
      var limit = this._max;
      var index = this.hashFn(key, limit);
      var bucket = this._storage[index]
      if (!bucket) {
        var bucket = [];
        this._storage[index] = bucket;
      }

      var override = false;
      for (var i = 0; i < bucket.length; i++) {
        var tuple = bucket[i];
        if (tuple[0] === key) {
          tuple[1] = value;
          override = true;
        }
      }
      if (!override) {
        bucket.push([key, value]);
        this._count++
          if (this._count > limit * 0.75) {
            this.resize(limit * 2);
          }
      }
      return this;
      return this._storage[this.hashFn(key, max)];
    }
  };
};

var newHash = makeHashTable();
newHash.insert(38)
var Stack = function() {
  
  var instance = Object.create(stackMethods);

  instance._storage = [];
  instance._size = 0;

  return instance;
  };

var stackMethods = {};


stackMethods.push = function(value) {
  this._storage.push(value);
};

stackMethods.pop = function(value) {
  return this._storage.pop()
};

var test = [1, 4, 5, 6, 7, 8]
var newStack = new Stack();

newStack.push(1)
newStack.push(2)
newStack.pop(1)
newStack.pop()
// first :: ([a], Number) -> a 
Array.prototype.first = function(n) {
  return n === undefined ? this[0] : this.slice(0, n);
};

// last :: [a] -> a
Array.prototype.last = function(n) {
  if (n > this.length) {
    return this;
  } else {
    return n === undefined
      ? this[this.length - 1]
        : this.slice(this.length - n, this.length);
  }
};


var nums = [1, 3, 4, 5];
nums.first();
nums.last()

assessment-week1

This Gist was automatically created by Carbide, a free online programming environment.

You can view a live, interactive version of this Gist here.