{"size": 1887, "ext": "js", "max_stars_repo_path": "data-structure/hash-table.js", "max_stars_repo_name": "alguerocode/data-structure-and-algorithms", "max_stars_repo_stars_event_min_datetime": "2021-07-05T08:21:33.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-05T08:21:55.000Z", "max_issues_repo_path": "data-structure/hash-table.js", "max_issues_repo_name": "alhashmicode/data-structure-and-algorithms", "max_issues_repo_issues_event_min_datetime": "2021-06-29T16:35:28.000Z", "max_issues_repo_issues_event_max_datetime": "2021-07-08T09:47:25.000Z", "max_forks_repo_path": "data-structure/hash-table.js", "max_forks_repo_name": "salah959/data-structure-and-algorithms", "max_forks_repo_forks_event_min_datetime": "2021-11-08T01:00:59.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-08T01:00:59.000Z"}
{"max_stars_count": 2, "max_issues_count": 5, "max_forks_count": 1, "avg_line_length": 25.8493150685, "max_line_length": 67, "alphanum_fraction": 0.5670376259}
// complete in sometime

class HashTable {
  constructor(size) {
    this.table = new Array(size);
    this.totalItems = 0;
  }
  _resize() {
    const newTable = new Array(this.table.length * 2);
    this.table.forEach(item => {
      if (item) {
        item.forEach(([key, value]) => {
          const idx = this._hashStringtoInt(key, newTable.length);
          if (newTable[idx]) {
            newTable[idx].push([key, value]);
          } else {
            newTable[idx] = [[key, value]];
          }
        });
      }
    })

    this.table = newTable;
  }
  _hashStringtoInt(str, tableSize) {
    let hash = 13;
    for (let i = 0; i < str.length; i++) {
      hash = (17 * hash * str.charCodeAt(i)) % tableSize;
    }
    return hash;
  }
  setItem(key, value) {
    const idx = this._hashStringtoInt(key, this.table.length);
    if (this.totalItems / this.table.length > 0.8) { // load factor
      this._resize();
    }
    if (this.table[idx]) {
      // if (this.table[idx].find(item => item[0] === key)) {
      //   return -1;
      // }
      this.table[idx].push([key, value]);
    } else {
      this.table[idx] = [[key, value]];
    }
    this.totalItems++;
  }
  getItem(key) {
    try {
      const idx = this._hashStringtoInt(key, this.table.length);
      return this.table[idx].find(arr => arr[0] === key)[1];
    } catch (error) {
      return undefined;
    }
  }
}
const hashTable = new HashTable(3);
console.log('----------------- result ------------------')
hashTable.setItem('lastName', 'saif');
hashTable.setItem('bro', 'omar');
hashTable.setItem('welcome', 'ahmed');
hashTable.setItem('hello', 'welcome');
hashTable.setItem('email', 'alhashmi');
hashTable.setItem('password', 'nobody');



console.log(hashTable.getItem('email'))
console.log(hashTable.getItem('name'))
console.log(hashTable.getItem('welcome'))
console.log(hashTable.getItem('this'))


