Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                
// Set up an object to hold our data
class Pair {
	constructor(key, value) {
		this.key = key;
		this.value = value;
	}
}

// Class for handling the hash table and associated functions
class HashTable {
	constructor(bucketCount) {
		this.buckets = [];
		this.bucketCount = bucketCount;
		this.oneOccupant = false;
		this.manyOccupants = false;
		
		for(let i = 0; i < bucketCount; i++) {
			this.buckets.push(undefined);
		}
	}
	
	hash(key) { // Simple hash function
		let hash = 0;
		for (var i = 0; i < key.length; i++) {
			hash += key.charCodeAt(i);
		}
		return hash % this.bucketCount;
	}
	
	state(index) {
		// Set up some variables to check buckets' state
		// Determine how many existing elements are in a bucket - we need to do this to determine whether or not to turn it into an array
		return { 
			isSingle: this.buckets[index] instanceof Pair, 
			isArray: this.buckets[index] instanceof Array 
		};	
	}
	
	insert(data) {
		let index = this.hash(data.key, this.bucketCount);
		
		if(!data.key) console.log("That's not a person");
		
		// First collision, create new array and add old and new occupants
		if(this.state(index).isSingle) { 
			let occupant = this.buckets[index]; // Hang onto existing occupant
			
			// Turn this bucket into an array (this is where you would put a linked list)
			// Then add the old occupant and new data to the array
			this.buckets[index] = []; 
			this.buckets[index].push(occupant, data); 
		
		// If there already was a collision, add person to array
		} else if(this.state(index).isArray) { 
			this.buckets.push(data);
		
		// If there are no entires in the bucket, create one
		} else { 
			this.buckets.splice(index, 1, data);
		}
	}
	
	remove(key) {
		let index = this.hash(key, this.bucketCount);
		
		if (!key) console.log("That's not a person");
		
		// If it's just a single item and not an array
		if(this.state(index).isSingle) {
			this.buckets[index] = undefined;	
			
		// If we need to search within an array:
		} else if(this.state(index).isArray) {
			
			// Adding complexity with a loop - linked list would be ideal!
			for(let i = 0; i < this.buckets[index].length; i++) {
				if(this.buckets[index][i].key === key) {
					this.buckets[index].splice(i, 1);	
				}
			}
		}
	}
	
	find(key) {
		let index = this.hash(key, this.bucketCount);
	
		if (this.buckets[index] === undefined) console.log(`${key} is nowhere to be found!`);
		
		// Same logic as previous two functions - account for an array, or not
		if(this.state(index).isSingle) {	
			console.log(`${key} exists in bucket number ${index}`);
		} else if(this.state(index).isArray) {
			
			// More complexity with a loop. Again, very ripe for linked lists!
			for(let i = 0; i < this.buckets[index].length; i++) {
				if(this.buckets[index][i].key == key) {
					console.log(`${key} exists in bucket and array number ${index} at ${i} in array`); 
				}
			}
		}
		
	} // END find
}

// Set up some data of my favorite people and their fake phone numbers
const lara = new Pair("Lara", "212-126-1262"),
			allie = new Pair("Allie", "212-731-7313"),
			ben = new Pair("Ben", "212-731-7313"),
			joyce = new Pair("Joyce", "212-888-8888"),
			jimmy = new Pair("Jimmy", "212-522-2211"),
			beth = new Pair("Bethany", "212-125-3221");

// Create the table with 10 buckets
const PeopleTable = new HashTable(10);

// Add some people
// In a real thing these would be inserted dynamically
PeopleTable.insert(ben);
PeopleTable.insert(allie);
PeopleTable.insert(joyce);
PeopleTable.insert(jimmy);
PeopleTable.insert(beth);

// Remove some people
PeopleTable.remove("Lara");
PeopleTable.remove("Joyce");

// Find some people
PeopleTable.find("Allie");
PeopleTable.find("Joyce"); // But she's been removed!

// Log the array for fun
console.dir(PeopleTable.buckets);

              
            
!
999px

Console