Object Oriented Programming Articles

Page 150 of 589

Rearrange string so that same character become n distance apart JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 324 Views

We need to rearrange a string so that identical characters are exactly n positions apart from each other. This problem requires careful character placement to maintain the specified distance constraint. For example, if we have the string "accessories" and n = 3, we need to ensure that each 's' character is exactly 3 positions away from other 's' characters, and the same applies to other repeated characters. Problem Understanding The algorithm works by: Counting the frequency of each character Sorting characters by frequency (most frequent first) Placing characters in a round-robin fashion with n distance apart ...

Read More

Object difference in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 187 Views

In JavaScript, finding the difference between two objects means identifying keys that exist in the first object but not in the second. This is useful for comparing data structures, tracking changes, or filtering object properties. Problem Statement We need to write a JavaScript function that takes two objects (possibly nested) and returns a new object containing only the key-value pairs that exist in the first object but are missing from the second object. Basic Implementation Here's a function that compares two objects and returns the difference: const obj1 = { "firstName": ...

Read More

Program to find largest of three numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 626 Views

We are required to write a JavaScript function that takes in three or more numbers and returns the largest of those numbers. For example: If the input numbers are − 4, 6, 7, 2, 3 Then the output should be − 7 Method 1: Using Math.max() with Spread Operator The simplest approach is using the built-in Math.max() function with the spread operator: const findLargest = (...nums) => { return Math.max(...nums); }; console.log(findLargest(4, 6, 7, 2, 3)); console.log(findLargest(15, 8, 23, 1)); ...

Read More

Search and update array based on key JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 240 Views

In JavaScript, you often need to merge data from two arrays based on a common property. This is useful when you have reference data in one array and need to enrich objects in another array. Consider these two arrays: let arr1 = [ {"LEVEL":4, "POSITION":"RGM"}, {"LEVEL":5, "POSITION":"GM"}, {"LEVEL":5, "POSITION":"GMH"} ]; let arr2 = [ {"EMAIL":"test1@stc.com", "POSITION":"GM"}, {"EMAIL":"test2@stc.com", "POSITION":"GMH"}, {"EMAIL":"test3@stc.com", "POSITION":"RGM"}, {"EMAIL":"test3@CSR.COM.AU", "POSITION":"GM"} ]; console.log("Original arr2:", arr2); ...

Read More

Converting days into years months and weeks - JavaScript

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

We are required to write a JavaScript function that takes in a number (representing the number of days) and returns an object with four properties: years, months, weeks, days The properties should have proper values that can be made from the total number of days. We assume a standard year has 365 days and each month has 30 days for simplicity. Problem Example If the input is 738 days, the output should be: { years: 2, months: 0, weeks: 1, days: 1 } ...

Read More

Loop backward in array of objects JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 210 Views

When working with arrays of objects in JavaScript, you may need to iterate through them in reverse order. This is useful for tasks like displaying data from newest to oldest or building strings that reverse the original order. We have an array of objects like this: let data = [ {id:1, Name: "Abe", RowNumber: 1 }, {id:2, Name: "Bob", RowNumber: 2 }, {id:3, Name: "Clair", RowNumber: 3 }, {id:4, Name: "Don", RowNumber: 3.0 }, {id:5, Name: "Edna", ...

Read More

Check Disarium number - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 447 Views

A Disarium number is a number where the sum of its digits raised to their respective positions equals the original number. Definition For a number with digits xy...z, it's a Disarium number if: xy...z = x^1 + y^2 + ... + z^n Where n is the total number of digits in the number. Example Let's check if 175 is a Disarium number: 175 = 1^1 + 7^2 + 5^3 = 1 + 49 + 125 = 175 Since the sum equals the original number, 175 is a Disarium ...

Read More

How to count digits of given number? JavaScript

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

The requirements here are simple, we are required to write a JavaScript function that takes in a number and returns the number of digits in it. For example − The number of digits in 4567 is 4 The number of digits in 423467 is 6 The number of digits in 457 is 3 Let's explore different approaches to count digits in JavaScript. Method 1: Using Recursive Approach This method uses recursion to divide the number by 10 until it reaches zero: const num = 2353454; const digits = (num, count = ...

Read More

Count total punctuations in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 756 Views

In JavaScript, you can count punctuation marks in a string by checking each character against a set of punctuation symbols. Common punctuation marks include periods, commas, semicolons, exclamation marks, question marks, and quotation marks. Common Punctuation Characters '!', ", ", "'", ";", '"', ".", "-", "?" Example Here's a JavaScript function that counts punctuation marks in a string: const str = "This, is a-sentence;.Is this a sentence?"; const countPunctuation = str => { const punct = "!, ';".?-"; let count = 0; ...

Read More

Access previously iterated element within array.map in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 381 Views

In JavaScript, you can access previously iterated elements within array.map() using the index parameter. The map() method provides both the current element and its index, allowing you to reference earlier elements in the array. Let's say the following is our array: var details = [ {subjectId:110, subjectName: 'Java' }, {subjectId:111, subjectName: 'Javascript' }, {subjectId:112, subjectName: 'MySQL' }, {subjectId:113, subjectName: 'MongoDB' } ]; Using Index to Access Previous Elements The key is to use the index parameter in the map() callback to access ...

Read More
Showing 1491–1500 of 5,881 articles
« Prev 1 148 149 150 151 152 589 Next »
Advertisements