Object Oriented Programming Articles

Page 186 of 589

Filter one array with another array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 401 Views

In JavaScript, filtering one array based on another array is a common task. This technique is useful when you need to keep only certain elements from an array based on criteria stored in another array. Problem Setup Consider the following arrays: const main = [ {name: "Karan", age: 34}, {name: "Aayush", age: 24}, {name: "Ameesh", age: 23}, {name: "Joy", age: 33}, {name: "Siddarth", age: 43}, {name: "Nakul", age: 31}, ...

Read More

The Zombie Apocalypse case study - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 303 Views

A nasty zombie virus is spreading out in the digital cities. We work at the digital CDC and our job is to look over the city maps and tell which areas are contaminated by the zombie virus so the digital army would know where to drop the bombs. They are the new kind of digital zombies which can travel only in vertical and horizontal directions and infect only numbers same as them. We'll be given a two-dimensional array with numbers in it. For some mysterious reason patient zero is always found in north west area of the ...

Read More

Finding the length of a JavaScript object

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 272 Views

JavaScript objects don't have a built-in length property like arrays. However, there are several methods to find the number of properties in an object. Suppose we have an object like this: const obj = { name: "Ramesh", age: 34, occupation: "HR Manager", address: "Tilak Nagar, New Delhi", experience: 13 }; We need to count the number of properties in this object. Using Object.keys() (Recommended) The most common and reliable method is Object.keys(), which returns an array of the object's property names: ...

Read More

Sorting an array of objects by property values - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 444 Views

In JavaScript, you can sort an array of objects by property values using the Array.sort() method with a custom comparison function. This is particularly useful when working with data structures like product catalogs, user lists, or any collection of objects. Sample Data Let's work with this array of home objects: const homes = [ { "h_id": "3", "city": "Dallas", "state": "TX", ...

Read More

Capitalize letter in a string in order and create an array to store - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 156 Views

We are required to write a JavaScript function that takes in a string and turns it into a Mexican Wave i.e. resembling string produced by successive capital letters in every word. For example, if the string is: const str = 'edabit'; Then the output should be the following with successive single capital letters: const output = ["Edabit", "eDabit", "edAbit", "edaBit", "edabIt", "edabiT"]; Method 1: Using String Prototype Extension This approach extends the String prototype with a custom replaceAt method: const str = 'edabit'; const replaceAt = function(index, ...

Read More

Recursion problem Snail Trail in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 214 Views

The snail trail problem involves traversing a 2D array in a spiral pattern from outside to inside. Given a square matrix, we need to create a flat array by following the spiral path: right → down → left → up, repeating until we reach the center. Suppose, we have an array like this: const arr = [ [1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7] ]; The array is bound to be a square matrix. We are required to ...

Read More

Find closest index of array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

When working with arrays in JavaScript, you might need to find the index of the element that is numerically closest to a given target value. This is useful in scenarios like finding the nearest data point, closest price match, or similar proximity-based searches. Problem Statement Given an array of numbers and a target value, we need to find the index of the array element that has the smallest absolute difference from the target. const arr = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362]; For example, if our target is 150, we ...

Read More

JavaScript Sleep() function?

vineeth.mariserla
vineeth.mariserla
Updated on 15-Mar-2026 53K+ Views

JavaScript doesn't have a built-in sleep() function like other languages (C's sleep(), Java's Thread.sleep(), Python's time.sleep()). However, we can create one using Promises and async/await. Creating a Sleep Function We can implement a sleep function using setTimeout() wrapped in a Promise: function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } Using Sleep with Promises function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } console.log("Start"); sleep(2000).then(() => { console.log("After 2 seconds"); }); Start ...

Read More

How to Create GUID / UUID in JavaScript?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 12K+ Views

A Globally Unique Identifier (GUID) or Universally Unique Identifier (UUID) is a 128-bit value used as a unique identifier in software development. It's typically represented as a 32-character hexadecimal string with hyphens, like: de305d84-75c4-431d-acc2-eb6b0e5f6014. Here are several JavaScript methods to generate GUIDs/UUIDs. Using Random Number Generation The most common approach uses Math.random() to generate a UUID v4 format: function generate_uuidv4() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var uuid = Math.random() * 16 | 0, v = c == 'x' ? uuid : (uuid & 0x3 ...

Read More

How to use finally on promise with then and catch in Javascript?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 2K+ Views

JavaScript asynchronous programming uses promise objects that don't block execution but signal when operations complete. Promises can either resolve successfully or reject with an error. The finally() method executes cleanup code regardless of the promise outcome, similar to finally blocks in synchronous try-catch statements. Syntax promise .then(result => { // handle success }) .catch(error => { // handle error }) .finally(() => { // ...

Read More
Showing 1851–1860 of 5,881 articles
« Prev 1 184 185 186 187 188 589 Next »
Advertisements