Object Oriented Programming Articles

Page 7 of 589

Product of numbers present in a nested array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 294 Views

We are required to write a JavaScript function that takes in an array of nested arrays of Numbers and some falsy values (including 0) and some strings as well and the function should return the product of number values present in the nested array. If the array contains some 0s, we should ignore them as well. Example The code for this will be − const arr = [ 1, 2, null, [ 2, 5, null, undefined, false, 5, [ ...

Read More

Finding sum of every nth element of array in JavaScript

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

We are required to write a JavaScript function that takes in an array of numbers and returns the cumulative sum of every number present at the index that is a multiple of n from the array. Understanding the Problem When we say "every nth element", we mean elements at indices 0, n, 2n, 3n, and so on. For example, if n=3, we sum elements at indices 0, 3, 6, 9, etc. Example Let's find the sum of every 3rd element from an array: const arr = [5, 3, 5, 6, 12, 5, 65, 3, ...

Read More

Smart concatenation of strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 211 Views

We need to write a JavaScript function that concatenates two strings with a smart approach. If the last character of the first string matches the first character of the second string, we omit one of those duplicate characters to avoid redundancy. Problem Statement When concatenating strings like "Food" and "dog", a simple join would give "Fooddog". However, since both strings share the character 'd' at the junction, smart concatenation removes the duplicate to produce "Foodog". Example Here's how to implement smart string concatenation: const str1 = 'Food'; const str2 = 'dog'; const concatenateStrings ...

Read More

Picking all the numbers present in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 172 Views

We are required to write a JavaScript function that takes in a string that contains some one-digit numbers in between and the function should return the sum of all the numbers present in the string. Example The code for this will be − const str = 'uyyudfgdfgf5jgdfj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3'; const sumNum = str => { const strArr = str.split(""); let res = 0; for(let i = 0; i < strArr.length; i++){ if(+strArr[i]){ ...

Read More

Greater possible digit difference of a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 226 Views

We are required to write a JavaScript function that takes in a number. Then the function should return the greatest difference that exists between any two digits of the number. In other words, the function should simply return the difference between the greatest and the smallest digit present in it. Example Problem If the number is 654646, Then the smallest digit here is 4 and the greatest is 6 Hence, our output should be 2 Using Recursive Approach The recursive solution extracts digits one by one and keeps track of minimum and maximum ...

Read More

Checking for special type of Arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 199 Views

We are required to write a JavaScript function that takes in an array of literals and checks if elements are the same or not if read from front or back. Such arrays are also known by the name of palindrome arrays. Some examples of palindrome arrays are: const arr1 = ['a', 'b', 'c', 'b', 'a']; const arr2 = [4, 7, 7, 4]; const arr3 = [7, 7, 7, 7, 7, 7]; Method 1: Using a For Loop This approach compares elements from both ends moving towards the center: const arr = [1, ...

Read More

Finding count of special characters in a string in JavaScript

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

In JavaScript, you can count special characters in a string by checking each character against a predefined set of special characters. This is useful for text validation, content analysis, or data processing tasks. Problem Statement We need to count occurrences of these special characters in a string: '!', ', ', ''', ';', '"', '.', '-', '?' The function should iterate through each character and increment a counter when it finds a match. Example Implementation const str = "This, is a-sentence;.Is this a sentence?"; const countSpecial = str => { ...

Read More

Keeping only redundant words in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 462 Views

We are required to write a JavaScript function that takes in a string and returns a new string with only the words that appeared more than once in the original string. Problem Statement If the input string is: const str = 'this is a is this string that contains that some repeating words'; Then the output should be: 'this is that' The function should identify duplicate words and return them in the order they first appeared. Using indexOf() and lastIndexOf() This approach checks if a word's first occurrence ...

Read More

Rounding off numbers to some nearest power in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 205 Views

We are required to write a JavaScript function that takes in a number and returns a number that can be represented as a power of 2 which is nearest to the input number. For example: If the input number is 145, the output should be 128 because 128 (which is 2^7) is the nearest power of 2 to 145. Understanding Powers of 2 Powers of 2 are numbers like: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512... Each number is double the previous one. Algorithm Approach The algorithm works by: ...

Read More

Code to construct an object from a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 330 Views

We are required to write a function that takes in a string as the first and the only argument and constructs an object with its keys based on the unique characters of the string and value of each key being defaulted to 0. For example: If the input string is − const str = 'hello world!'; Output Then the output object should be − const obj = { "h": 0, "e": 0, "l": 0, "o": 0, " ": 0, "w": 0, "r": 0, "d": 0, "!": 0 }; Using Array.reduce() ...

Read More
Showing 61–70 of 5,881 articles
« Prev 1 5 6 7 8 9 589 Next »
Advertisements