Front End Technology Articles

Page 369 of 652

Multi Dimensional Arrays in Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 409 Views

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 More

Corner digit number difference - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 170 Views

We are required to write a JavaScript function that takes in a number, constructs a new number from the first and last digit of that number and returns the difference between the original number and the number thus formed. For example: If the input is 34567 Then the corner digits number will be: 37 And the output will be: 34530 Algorithm The solution involves extracting the first and last digits, combining them to form a corner number, then calculating the difference. Example Following is the code: ...

Read More

Clearing the elements of a Stack in Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 1K+ Views

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 More

Check three consecutive numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 551 Views

We are required to write a JavaScript function that takes in a Number, say n, and we are required to check whether there exist such three consecutive natural numbers (not decimal/floating point) whose sum equals to n. If there exist such numbers, our function should return them, otherwise it should return false. Following is the code − How It Works For three consecutive numbers to sum to n, we need: x + (x+1) + (x+2) = n, which simplifies to 3x + 3 = n. Therefore, n must be divisible by 3 and greater than 5 (since ...

Read More

Creating a Queue in Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 795 Views

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 More

Remove elements from a queue using Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 1K+ Views

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 More

Creating a Priority Queue using Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 574 Views

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 More

Add elements to a PriorityQueue using Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 272 Views

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 More

Sum of distinct elements of an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 568 Views

We are required to write a JavaScript function that takes in one such array and counts the sum of all distinct elements of the array. For example: Suppose, we have an array of numbers like this − const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1]; The distinct elements are: 1, 5, 2, 3, 4, 7, 8. Their sum is: 1 + 5 + 2 + 3 + 4 + 7 + 8 = 30. Using lastIndexOf() Method This approach checks if the current index matches the ...

Read More

Remove elements from a PriorityQueue using Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 394 Views

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 More
Showing 3681–3690 of 6,519 articles
« Prev 1 367 368 369 370 371 652 Next »
Advertisements