Object Oriented Programming Articles

Page 25 of 589

Calculating quarterly and yearly average through JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 310 Views

In this tutorial, we'll learn how to calculate quarterly and yearly averages from an array of numbers using JavaScript. This involves chunking data into groups and computing their averages. Suppose we have an array of numbers like this: const arr = [1, 2, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; console.log("Original array:", arr); Original array: [1, 2, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] We need to group this array ...

Read More

Recursive Staircase problem in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 731 Views

The staircase problem is a classic dynamic programming problem in computer science. Given n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 or 2 stairs at a time, and we need to count the number of ways to reach the top. We'll write a JavaScript function that takes a number n representing the number of stairs and returns the total number of ways to climb them. Understanding the Problem This problem follows the Fibonacci sequence pattern. For n stairs: 1 stair: 1 way (take 1 step) ...

Read More

Group strings starting with similar number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 312 Views

In JavaScript, you can group strings that start with the same number by extracting the first part before the decimal and comparing consecutive elements. This is useful when working with version numbers, decimal classifications, or any structured string data. Problem Statement Given an array of number strings like this: const arr = ["1.1", "1.2", "1.3", "2.1", "2.2", "3.1", "3.2", "3.3", "4.1", "4.2"]; console.log("Input array:", arr); Input array: [ '1.1', '1.2', '1.3', '2.1', '2.2', '3.1', '3.2', '3.3', '4.1', '4.2' ] We need to group all strings starting with the same number into ...

Read More

Uncamelising a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 255 Views

We are required to write a JavaScript function that takes in a string as the first argument and a separator character as the second argument. The first string is guaranteed to be a camelCased string. The function should convert the case of the string by separating the words by the separator provided as the second argument. For example − If the input string is − const str = 'thisIsAString'; const separator = '_'; Then the output string should be − const output = 'this_is_a_string'; Using Custom Logic This ...

Read More

Replace all occurrence of specific words in a sentence based on an array of words in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 553 Views

We are required to write a JavaScript function that takes a string and an array of strings. Our function should return a new string, where all the occurrences of the word in the string that are present in the array are replaced by a whitespace. Our function should use the String.prototype.replace() method to solve this problem. Understanding the Problem When filtering words from a sentence, we need to: Match whole words only (not parts of words) Handle case-insensitive matching Use regular expressions ...

Read More

Reversing a string using for loop in JavaScript

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

We are required to write a JavaScript function that takes in a string as the only argument. The function should construct a new reversed string based on the input string using a for loop. Syntax for (let i = string.length - 1; i >= 0; i--) { reversedString += string[i]; } Example Following is the code − const str = 'this is the original string'; const reverseString = (str = '') => { let reverse = ''; const { length: ...

Read More

How to merge two strings alternatively in JavaScript

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

In JavaScript, merging two strings alternatively means combining characters from both strings one by one. This creates a new string where characters alternate between the first and second input strings. For example, if we have two strings: const str1 = 'abc'; const str2 = 'def'; console.log('String 1:', str1); console.log('String 2:', str2); String 1: abc String 2: def The expected output should be: adbecf Using Loop-Based Approach Here's a function that merges two strings alternatively by iterating through both strings simultaneously: const str1 = 'abc'; const ...

Read More

Difference between sum and product of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 260 Views

We are required to write a JavaScript function that takes in an array of Numbers as the only argument. The function should calculate the sum of all numbers in the array and the product of all numbers. Then the function should return the absolute difference between the sum and the product. Example Following is the code − const arr = [1, 4, 1, 2, 1, 6, 3]; const sumProductDifference = (arr = []) => { const creds = arr.reduce((acc, val) => { let { ...

Read More

Removing duplicate objects from array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 349 Views

Removing duplicate objects from arrays is a common requirement in JavaScript applications. Let's explore different approaches to eliminate duplicate objects while preserving the original data structure. The Problem Consider an array containing duplicate objects: const arr = [ {"title": "Assistant"}, {"month": "July"}, {"event": "Holiday"}, {"title": "Assistant"} ]; console.log("Original array:", arr); Original array: [ { title: 'Assistant' }, { month: 'July' }, { event: 'Holiday' }, { title: 'Assistant' ...

Read More

Flattening a deeply nested array of literals in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 420 Views

We are required to write a JavaScript function that takes in a nested array of literals as the only argument. The function should construct a new array that contains all the literal elements present in the input array but without nesting. For example − If the input array is − const arr = [ 1, 3, [5, 6, [7, [6, 5], 4], 3], [4] ]; Then the output array should be − [1, 3, 5, 6, 7, 6, 5, 4, 3, 4] Method 1: Recursive Approach ...

Read More
Showing 241–250 of 5,881 articles
« Prev 1 23 24 25 26 27 589 Next »
Advertisements