Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Javascript Articles
Page 396 of 534
Multi Dimensional Arrays in Javascript
Multi-dimensional arrays in JavaScript are arrays that contain other arrays as elements. They're useful when you need to organize data in rows and columns, like storing temperatures for each day of the week at different time intervals. Creating Multi-Dimensional Arrays Instead of creating separate arrays for each day: let monday = [35, 28, 29, 31]; let tuesday = [33, 24, 25, 29]; console.log("Monday temperatures:", monday); console.log("Tuesday temperatures:", tuesday); Monday temperatures: [ 35, 28, 29, 31 ] Tuesday temperatures: [ 33, 24, 25, 29 ] You can use a multi-dimensional array to ...
Read MoreClearing the elements of a Stack in Javascript
Consider a simple stack class in JavaScript. We'll implement a clear method to remove all elements from the stack. Basic Stack Implementation class Stack { constructor(maxSize) { // Set default max size if not provided if (isNaN(maxSize)) { maxSize = 10; } this.maxSize = maxSize; ...
Read MoreCreating a Queue in Javascript
Though Arrays in JavaScript provide all the functionality of a Queue, let us implement our own Queue class. Our class will have the following functions − enqueue(element): Function to add an element in the queue. dequeue(): Function that removes an element from the queue. peek(): Returns the element from the front of the queue. isFull(): Checks if we reached the element limit on the queue. isEmpty(): checks if the queue is empty. clear(): Remove all elements. display(): display all contents of the array Let's start by defining a simple class with a constructor that takes the ...
Read MoreRemove elements from a queue using Javascript
Dequeuing elements from a Queue means removing them from the front/head of the queue. We are taking the start of the container array to be the head of the queue as we'll perform all operations with respect to it. 10 20 30 40 FRONT REAR ...
Read MoreCreating a Priority Queue using Javascript
A priority queue is a data structure where each element has a priority. Elements are removed based on their priority rather than insertion order. Here's how to implement one using JavaScript arrays. Priority Queue Structure Our priority queue class will include these essential methods: enqueue(element, priority): Add an element with a specific priority dequeue(): Remove and return the highest priority element peek(): Return the highest priority element without removing it isEmpty(): Check if the queue is empty isFull(): Check if the queue has reached capacity display(): Show all queue contents Basic Implementation Let's ...
Read MoreAdd elements to a PriorityQueue using Javascript
Enqueuing elements to a PriorityQueue means adding them in the array in order of the priority of the element. We'll consider higher numbers to be higher priorities. We'll loop through the container till we find a lower priority and then add the element there. If not, then we'll push it at the end of the container. Note that we're creating the element object with the data and priority. Hence we can implement the enqueue function as follows: The enqueue() Method enqueue(data, priority) { // Check if Queue is full ...
Read MoreRemove elements from a PriorityQueue using Javascript
Dequeuing elements from a PriorityQueue means removing the element with the highest priority. Since we store elements with the highest priority at the end of the array, we can simply pop it to dequeue it. Priority Queue Structure World Priority: 2 Hello Priority: 3 Foo Priority: 8 Highest Priority ...
Read MorePeeking elements from a PriorityQueue using JavaScript
Peeking a PriorityQueue means getting the element with the highest priority without removing it from the queue. This operation is useful when you need to inspect the next item to be processed without actually processing it. The peek() Method Implementation The peek function returns the element with the highest priority. In our implementation, elements are stored in ascending order of priority, so the last element has the highest priority: peek() { if (this.isEmpty()) { console.log("Queue Underflow!"); ...
Read MoreThe PriorityQueue Class in Javascript
A Priority Queue is a data structure where each element has an associated priority. Elements with higher priority are served before elements with lower priority. In JavaScript, we can implement this using a class-based approach. Complete PriorityQueue Implementation Here's a complete implementation of the PriorityQueue class with all essential methods: class PriorityQueue { constructor(maxSize) { // Set default max size if not provided if (isNaN(maxSize)) { maxSize = 10; ...
Read MoreBasic Operations supported by a list in Javascript
JavaScript arrays (lists) support several fundamental operations that allow you to manipulate and access data efficiently. Here are the core operations you can perform on arrays. Basic List Operations Insertion − Add elements at the beginning, middle, or end of the list Deletion − Remove elements from any position in the list Display − Show the complete list contents Search − Find elements using specific values or conditions Access − Retrieve elements by their index position Insertion Operations You ...
Read More