Object Oriented Programming Articles

Page 121 of 589

Ordering string in an array according to a number in the string JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 142 Views

We have an array of strings each of which contain one or more numbers like this − const arr = ['di5aster', 'ca1amity', 'cod3', 'ho2me3', 'ca11ing']; We are required to write a sorting function that sorts this array in ascending order of the numbers present in the strings. The correct order will be − const output = [ 'ca1amity', 'cod3', 'di5aster', 'ca11ing', 'ho2me3' ]; Therefore, let's write the code for this problem − Understanding the Problem Each string contains embedded numbers. We need to extract these numbers and sort the array ...

Read More

How to pass event objects from one function to another in JavaScript?

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

In JavaScript, event objects contain information about user interactions like clicks, key presses, or mouse movements. You can pass these event objects between functions to share event data and manipulate the same target element across multiple functions. Syntax // Function that receives event and passes it to another function firstFunction(event) { // Do something with event secondFunction(event); // Pass event to another function } function secondFunction(event) { // Use the same event object event.target.style.property = "value"; } ...

Read More

How to get the maximum count of repeated letters in a string? JavaScript

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

We have a string that contains some repeated letters like this: const a = "fdsfjngjkdsfhhhhhhhhhhhfsdfsd"; Our job is to write a function that returns the count of maximum consecutive same letters in a streak. Like in the above string the letter 'h' appears for 11 times in a row consecutively, so our function should return 11 for this string. This problem is a good candidate for the sliding window algorithm, where a stable window contains consecutive identical letters and one that contains different elements is unstable. The window adjusts by moving the start pointer when different characters ...

Read More

How do we loop through array of arrays containing objects in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 596 Views

In JavaScript, when working with nested arrays containing objects, you need to use nested loops to access each object's properties. This tutorial demonstrates multiple approaches to iterate through such complex data structures. Understanding the Data Structure An array of arrays containing objects has this structure: let arrObj = [ [ { name: "Rohan", age: 22 }, { name: "Mohan", age: 12 } ], [ ...

Read More

Implementation of Stack in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 538 Views

A stack is a Last In First Out (LIFO) data structure where elements are added and removed from the same end, called the top. This article demonstrates how to implement a stack in JavaScript using a class-based approach with basic operations like push, pop, and display. What is a Stack? A stack follows the LIFO principle - the last element added is the first one to be removed. Think of it like a stack of plates where you can only add or remove plates from the top. Item 3 ...

Read More

Shared properties in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 445 Views

In JavaScript, properties can be shared across all instances of an object by attaching them to the constructor function's prototype property. This creates a single shared property that all instances can access, rather than each instance having its own copy. How Prototype Properties Work When you access a property on an object, JavaScript first checks if the property exists on the instance itself. If not found, it looks up the prototype chain to find the property on the constructor's prototype. Student.prototype school: "St Marks" ...

Read More

Share methods in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 708 Views

Methods can be shared across multiple object instances by attaching them to the prototype property. This approach ensures all instances of a constructor function share the same method, making memory usage more efficient than defining methods inside the constructor. Basic Prototype Method Sharing When you define a method on the prototype, all instances created from that constructor function can access it: Shared Methods Shared Methods in JavaScript ...

Read More

Fetch Second minimum element from an array without sorting JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 547 Views

We have an array of Numbers, and we are required to write a function that returns the second smallest value from the array. For example − if the array is − const arr = [67, 87, 56, 8, 56, 78, 54, 67, 98, 56, 54]; Then the output should be the following − 54 because 54 is the smallest value after 8 Method 1: Using indexOf and splice This approach finds the minimum element, removes it from a copy of the array, then finds the minimum of the remaining ...

Read More

Static Properties in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 190 Views

Static properties in JavaScript belong to the class itself rather than its instances. They can be accessed directly on the class without creating objects, making them useful for storing class-level data like constants or shared values. Syntax // Function constructor approach function ClassName(params) { // instance properties } ClassName.staticProperty = value; // ES6 class approach class ClassName { static staticProperty = value; } Example: Static Properties with Function Constructor ...

Read More

How to check if an object is an instance of a Class in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 469 Views

In JavaScript, you can check if an object is an instance of a specific class using the instanceof operator. This operator returns true if the object was created by the specified constructor function or class. Syntax object instanceof Constructor Using instanceof with Constructor Functions Instance Check Example CHECK INSTANCE function Student(name, age, standard) { this.name = name; this.age = age; this.standard = standard; } let student1 = new Student("Rohan", ...

Read More
Showing 1201–1210 of 5,881 articles
« Prev 1 119 120 121 122 123 589 Next »
Advertisements