Object Oriented Programming Articles

Page 14 of 589

How to merge specific elements inside an array together - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 175 Views

When working with arrays containing mixed data types, you might need to merge consecutive numeric elements while keeping certain separators intact. This is common when processing data that represents grouped numbers. Let's say we have the following array: var values = [7, 5, 3, 8, 9, '/', 9, 5, 8, 2, '/', 3, 4, 8]; console.log("Original array:", values); Original array: [7, 5, 3, 8, 9, '/', 9, 5, 8, 2, '/', 3, 4, 8] Using join() and split() Method To merge specific elements while preserving separators, we can use join(), split(), ...

Read More

Wrap object properties of type string with arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 595 Views

When working with objects, you may need to ensure all properties are arrays. This is useful for normalizing data structures where some properties might be strings while others are already arrays. The Problem Consider an object where some properties are strings and others are arrays. To process them uniformly, you need all properties to be arrays: var details = { name: ["John", "David"], age1: "21", age2: "23" }; console.log("Original object:"); console.log(details); Original object: { name: [ ...

Read More

Array filtering using first string letter in JavaScript

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

Suppose we have an array that contains names of some people like this: const arr = ['Amy', 'Dolly', 'Jason', 'Madison', 'Patricia']; console.log(arr); [ 'Amy', 'Dolly', 'Jason', 'Madison', 'Patricia' ] We are required to write a JavaScript function that takes in one such array as the first argument, and two lowercase alphabet characters as second and third arguments. Then, our function should filter the array to contain only those elements that start with alphabets that fall within the range specified by the second and third arguments. Therefore, if the second and third arguments ...

Read More

Number of vowels within an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 805 Views

We are required to write a JavaScript function that takes in an array of strings, (they may be a single character or greater than that). Our function should simply count all the vowels contained in the array. Example Let us write the code − const arr = ['Amy', 'Dolly', 'Jason', 'Madison', 'Patricia']; const countVowels = (arr = []) => { const legend = 'aeiou'; const isVowel = c => legend.includes(c.toLowerCase()); let count = 0; arr.forEach(el => { for(let ...

Read More

Comparing objects in JavaScript and return array of common keys having common values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 841 Views

We are required to write a JavaScript function that takes in two objects. The function should return an array of all those common keys that have common values across both objects. Example The code for this will be − const obj1 = { a: true, b: false, c: "foo" }; const obj2 = { a: false, b: false, c: "foo" }; const compareObjects = (obj1 = {}, obj2 = {}) => { const common = Object.keys(obj1).filter(key => { if(obj1[key] === obj2[key] && obj2.hasOwnProperty(key)){ ...

Read More

Dividing a floating number, rounding it to 2 decimals, and calculating the remainder in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 911 Views

When dividing a floating-point number, you often need to round the result to a specific number of decimal places and handle any remainder. This is useful for financial calculations, splitting costs, or distributing values evenly. Problem Suppose we have a floating-point number: 2.74 If we divide this number by 4, the result is 0.685. We want to divide this number by 4 but the result should be rounded to 2 decimals. Therefore, the result should be: 3 times 0.69 and a remainder of 0.67 Solution Using toFixed() The ...

Read More

Converting numbers to Indian currency using JavaScript

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

Converting numbers to Indian currency format is a common requirement in web applications. JavaScript provides a built-in method to format numbers according to different locales, including the Indian numbering system. Understanding Indian Currency Format Indian currency uses a unique grouping system where numbers are grouped by hundreds (lakhs and crores) rather than thousands. For example: 1000 → ₹1, 000.00 129943 → ₹1, 29, 943.00 76768798 → ₹7, 67, 68, 798.00 Using toLocaleString() Method The toLocaleString() method with Indian locale ('en-IN') automatically handles the currency formatting: const num1 = 1000; const num2 ...

Read More

Finding average in mixed data type array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 441 Views

Suppose, we have an array of mixed data types like this − const arr = [1, 2, 3, 4, 5, "4", "12", "2", 6, 7, "4", 3, "2"]; We are required to write a JavaScript function that takes in one such array and returns the average of all such elements that are a number or can be partially or fully converted to a number. The string "3454fdf", isn't included in the problem array, but if it wasn't there, we would have used the number 3454 as its contribution to average. Example The code ...

Read More

Function to add up all the natural numbers from 1 to num in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 344 Views

We are required to write a JavaScript function that takes in a number, say num. Then our function should return the sum of all the natural numbers between 1 and num, including 1 and num. For example, if num is − const num = 5; Then the output should be − const output = 15; because, 1+2+3+4+5 = 15 The Mathematical Formula We will use the efficient mathematical formula to solve this problem − Sum of all natural numbers up to n = ...

Read More

Check if an array is growing by the same margin in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 195 Views

We are required to write a JavaScript function that takes in an array of numbers. Our function should return true if the difference between all adjacent elements is the same positive number, false otherwise. Syntax function growingMarginally(arr) { // Check if array has consistent positive differences // Return true/false } Example: Basic Implementation The code for this will be − const arr = [4, 7, 10, 13, 16, 19, 22]; const growingMarginally = arr => { if(arr.length

Read More
Showing 131–140 of 5,881 articles
« Prev 1 12 13 14 15 16 589 Next »
Advertisements