Articles on Trending Technologies

Technical articles with clear explanations and examples

Creating a hash table using Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Jun-2020 463 Views

Let us set up a simple class that we'll use to define all these methods on. We'll create a container object to store the hash table and create a display function to display the table. Note that for collision resolution, we'll use chaining.The display function takes each entry (hashed value) in the table and prints all pairs associated with it.ExampleWe'll also create a new class on the prototype to store the key-value pairs.class HashTable {    constructor() {       this.container = [];       // Populate the container with empty arrays       // which can ...

Read More

Add elements to a hash table using Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Jun-2020 1K+ Views

When adding elements to a hash table the most crucial part is collision resolution. We're going to use chaining for the same. There are other algorithms you can read about here: https://en.wikipedia.org/wiki/Hash_table#Collision_resolutionNow let's look at the implementation. We'll be creating a hash function that'll work on integers only to keep this simple. But a more complex algorithm can be used to hash every object − Exampleput(key, value) {    let hashCode = hash(key);    for(let i = 0; i < this.container[hashCode].length; i ++) {       // Replace the existing value with the given key       ...

Read More

Search Element in an Javascript Hash Table

Sai Subramanyam
Sai Subramanyam
Updated on 15-Jun-2020 457 Views

We've kind of already implemented this in our put method. Let us look at it again in isolation.Exampleget(key) {    let hashCode = hash(key);    for(let i = 0; i < this.container[hashCode].length; i ++) {       // Find the element in the chain       if(this.container[hashCode][i].key === key) {          return this.container[hashCode][i];       }    }    return undefined; }You can test it using.Examplelet ht = new HashTable(); ht.put(10, 94); ht.put(20, 72); ht.put(30, 1); ht.put(21, 6); ht.put(15, 21); ht.put(32, 34); console.log(ht.get(20)); console.log(ht.get(21)); console.log(ht.get(55)); console.log(ht.get(32));OutputThis will give the output.{ key: 20, ...

Read More

How to give a limit to the input field in HTML?

Rishi Raj
Rishi Raj
Updated on 15-Jun-2020 6K+ Views

The HTML tag is used to get user input in HTML. To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively.The max and min attributes are used with number, range, date, datetime, datetime-local, month, time and week input types.ExampleYou can try to run the following code to give a limit to the input field in HTML −           HTML input number                        Mention any number between 1 to 10                              

Read More

Remove elements from Javascript Hash Table

karthikeya Boyini
karthikeya Boyini
Updated on 15-Jun-2020 1K+ Views

To remove elements, we simply need to find them and remove them using a simple splice function call that removes elements in place from an array.Let us look at the implementation of the same − Exampleremove(key) {    let hashCode = this.hash(key);    for (let i = 0; i < this.container[hashCode].length; i++) {       // Find the element in the chain       if (this.container[hashCode][i].key === key) {          this.container[hashCode].splice(i, 1);          return true       }    }    return false; }You can test this using − Examplelet ht = ...

Read More

How can I show and hide div on mouse click using jQuery?

David Meador
David Meador
Updated on 15-Jun-2020 20K+ Views

To show and hide div on mouse click using jQuery, use the toggle() method. On mouse click, the div is visible and on again clicking the div, it hides.ExampleLive Demo $(document).ready(function(){     $('#show').click(function() {       $('.menu').toggle("slide");     }); }); Click to Show/ Hide div             India       US       UK       Australia      

Read More

Loop through a hash table using Javascript

Sai Subramanyam
Sai Subramanyam
Updated on 15-Jun-2020 2K+ Views

Now let us create a forEach function that'll allow us to loop over all key-value pairs and call a callback on those values. For this, we just need to loop over each chain in the container and call the callback on the key and value pairs.ExampleforEach(callback) {    // For each chain    this.container.forEach(elem => {       // For each element in each chain call callback on KV pair       elem.forEach(({ key, value }) => callback(key, value));    }); }You can test this using.Examplelet ht = new HashTable(); ht.put(10, 94); ht.put(20, 72); ht.put(30, 1); ht.put(21, 6); ...

Read More

Joining two hash tables in Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Jun-2020 656 Views

Sometimes we need to combine containers together using a join function and get a new container. We'll write a static join method that takes in 2 HashTables and creates a new HashTable with all the values. For the sake of simplicity, we'll let the values from the second one override the values for the first one if there are any keys present in both of them. Examplestatic join(table1, table2) {    // Check if both args are HashTables    if(!table1 instanceof HashTable || !table2 instanceof HashTable) {       throw new Error("Illegal Arguments")    }    let combo = ...

Read More

The HashTable Class in Javascript

Sai Subramanyam
Sai Subramanyam
Updated on 15-Jun-2020 197 Views

Here is the complete implementation of the HashTable class. This could of course be improved by using more efficient data structures and collision resolution algorithms.Exampleclass HashTable {    constructor() {       this.container = [];       // Populate the container with empty arrays       // which can be used to add more elements in       // cases of collisions       for (let i = 0; i < 11; i++) {          this.container.push([]);       }    }    display() {       this.container.forEach((value, index) => ...

Read More

How can I show and hide an HTML element using jQuery?

David Meador
David Meador
Updated on 15-Jun-2020 1K+ Views

To hide and show an HTML element using jQuery, use the hide and show() methods. Here, the element is hidden and shown on button click,ExampleLive Demo $(document).ready(function(){     $("#visible").click(function(){         $("p").show();     });      $("#hidden").click(function(){         $("p").hide();     }); }); This text  will show on clicking "Show element" button, and hide on clicking "Hide element" button. Hide element Show element

Read More
Showing 45811–45820 of 61,248 articles
Advertisements