Object Oriented Programming Articles

Page 44 of 589

How to count the occurrence of a specific string in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 275 Views

In JavaScript, counting occurrences of a substring within a string is a common task. There are several approaches to accomplish this, from simple loops to built-in string methods. For example, counting how many times "is" appears in "this is a string" should return 2. count('this is a string', 'is') should return 2; Method 1: Using indexOf() with Loop The most reliable approach uses indexOf() to find each occurrence and increment a counter: const str1 = 'this is a string'; const str2 = 'is'; function countOccurrences(mainStr, subStr) { ...

Read More

Iterating through an array, adding occurrences of a true in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 179 Views

Suppose we have an array of true/false values represented by 't'/'f' which we retrieved from some database like this − const arr = ['f', 't', 'f', 't', 't', 't', 'f', 'f', 't', 't', 't', 't', 't', 't', 'f', 't']; console.log(arr); [ 'f', 't', 'f', 't', 't', 't', 'f', 'f', 't', 't', 't', 't', 't', 't', 'f', 't' ] We need to count consecutive occurrences of 't' that are sandwiched between two 'f's and return an array of those counts. Array: ['f', 't', 'f', 't', 't', 't', 'f', 'f', 't', ...

Read More

Find specific key value in array of objects using JavaScript

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

When working with JavaScript objects containing arrays of nested objects, you often need to find which parent key contains an object with specific property values. This is common when dealing with product catalogs, user groups, or categorized data. Example Data Structure Consider this product catalog object where each category contains an array of products: const obj = { "LAPTOP": [{ "productId": "123" }], "DESKTOP": [{ "productId": "456" ...

Read More

Sum of array object property values in new array of objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 762 Views

When working with arrays of objects, we often need to group objects by a common property and sum up their numeric values. This is particularly useful when dealing with student records, sales data, or any scenario where duplicate categories need to be consolidated. Suppose we have an array of objects that contains data about students and their marks: const arr = [ { subject: 'Maths', marks: '40', noOfStudents: '5' }, { subject: 'Science', marks: '50', noOfStudents: '16' }, { subject: 'History', marks: '35', noOfStudents: '23' }, { subject: 'Science', ...

Read More

Calculating Josephus Permutations efficiently in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 415 Views

This problem takes its name from arguably the most important event in the life of the ancient historian Josephus. According to his tale, he and his 40 soldiers were trapped in a cave by the Romans during a siege. Refusing to surrender to the enemy, they instead opted for mass suicide, with a twist — they formed a circle and proceeded to kill one man every three, until one last man was left (and that it was supposed to kill himself to end the act). Josephus and another man were the last two and, as we now know ...

Read More

How to split comma and semicolon separated string into a two-dimensional array in JavaScript ?

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

Let's say we have a variable "users" that contains the following string of text where each user is separated by a semicolon and each attribute of each users is separated by a comma − const users = 'Bob, 1234, Bob@example.com;Mark, 5678, Mark@example.com'; We are required to write a JavaScript function that takes in one such string and splits this into a multidimensional array that looks like this − const arr = [ ['Bob', 1234, 'Bob@example.com'], ['Mark', 5678, 'Mark@example.com'] ]; Method 1: Using split() and for Loop ...

Read More

Finding the least common multiple of a range of numbers in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 318 Views

We are required to write a JavaScript function that takes in an array of exactly two numbers specifying a range. The function should then calculate the least common multiple of all the numbers within that range and return the final result. Understanding LCM and GCD To find the LCM of multiple numbers, we first need helper functions for GCD (Greatest Common Divisor) and LCM of two numbers: GCD: The largest number that divides both numbers evenly LCM: The smallest positive number that is divisible by both numbers Formula: LCM(a, b) = (a × b) / ...

Read More

How to find a value is present in binary tree or not in JavaScript ?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 316 Views

We are required to write a JavaScript function on the prototype object of a BinarySearchTree data type that takes in a value and finds whether or not that value is contained in the BST. Binary Search Tree Structure A Binary Search Tree (BST) is a tree data structure where each node has at most two children. The left child contains values smaller than the parent, and the right child contains values greater than the parent. 50 30 ...

Read More

Finding word starting with specific letter in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 716 Views

Finding words that start with a specific letter is a common requirement in JavaScript applications. This guide shows different approaches to locate the first array element beginning with a specified character. Problem Statement We need to write a JavaScript function that takes an array of strings and a character, then returns the index of the first string starting with that character. Using substring() Method The substring() method extracts the first character for comparison: const names = ['Naman', 'Kartik', 'Anmol', 'Rajat', 'Keshav', 'Harsh', 'Suresh', 'Rahul']; const firstIndexOf = (arr = [], char = '') ...

Read More

Merge two sorted arrays to form a resultant sorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 274 Views

We are required to write a JavaScript function that takes in two sorted arrays of numbers. The function should merge the two arrays together to form a resultant sorted array and return that array. For example, if the two arrays are: const arr1 = [2, 6, 6, 8, 9]; const arr2 = [1, 4, 5, 7]; Then the output array should be: [1, 2, 4, 5, 6, 6, 7, 8, 9] Method 1: Using Two Pointers Technique The most efficient approach is the two-pointers technique, which compares elements from both ...

Read More
Showing 431–440 of 5,881 articles
« Prev 1 42 43 44 45 46 589 Next »
Advertisements