<html>
<head>
  <meta charset="utf-8">
  <title>Mocha Tests</title>
  <link href="https://unpkg.com/mocha@5.2.0/mocha.css" rel="stylesheet" />
</head>
<body>
  <div id="mocha"></div>

  <script src="https://unpkg.com/chai/chai.js"></script>
  <script src="https://unpkg.com/mocha@5.2.0/mocha.js"></script>

  <div></div>
  

</body>
</html>


class HashTable{
  
  constructor(size=42){
    this.buckets =  new Array(size)
    this.size = size
  }
  
  
    hash(key){
       return key.toString().length % this.size;
   }
  
  
  set(key,value){
    
    let index = this.hash(key);
    
    if(!this.buckets[index]){
      this.buckets[index] = [];
    }
    
    this.buckets[index].push([key,value])
    
    return index
    
  }
  
  get(key){
    
    let index = this.hash(key);
  
     if(!this.buckets[index])return null
     
        for(let bucket of this.buckets[index]){
             // key
          if(bucket [0] === key){
             //value
            return bucket [1]
           }
        }
        
      
  }
  
}



mocha.setup('bdd');


var assert = chai.assert;

describe('Hash tables',()=>{
  
  it("should add the new data ",()=>{
    
     const hasht = new HashTable(10);
       
     hasht.set("userId3","pop")
     hasht.set("userId4","king")
     hasht.set("userId9","april")
     hasht.set("userId50","gowtham")
     hasht.set("userId100","olo")
   
     assert.deepEqual([["userId3", "pop"], ["userId4", "king"], ["userId9", "april"]],hasht.buckets[7])
     
  })

  it("should  get the value ",()=>{
     const hasht = new HashTable(10);
       
     hasht.set("userId3","pop")
     hasht.set("userId4","king")
     hasht.set("userId9","april")
     hasht.set("userId50","gowtham")
     hasht.set("userId100","olo")
   
     assert.equal("gowtham",hasht.get("userId50"))
     assert.equal("pop",hasht.get("userId3"))
     assert.equal("king",hasht.get("userId4"))
     assert.equal("olo",hasht.get("userId100"))
     assert.equal("april",hasht.get("userId9"))
    
    
  })
  
})



mocha.run()

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.