Object Oriented Programming Articles

Page 11 of 589

Adding up identical elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 141 Views

We are required to write a JavaScript function that takes in an array of numbers and sums all the identical numbers together at one index. Problem Overview When we have an array with duplicate values, we want to combine them into a single occurrence with their sum. The first occurrence of each unique number will contain the total sum of all occurrences. Example Input and Output If the input array is: const arr = [20, 10, 15, 20, 15, 10]; Then the output should be: const output = [40, 20, ...

Read More

Squared concatenation of a Number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 294 Views

We are required to write a JavaScript function that takes in a number and returns a new number in which all the digits of the original number are squared and concatenated. For example: If the number is − 99 Then the output should be − 8181 because 9² is 81 and 9² is 81, so concatenating them gives 8181. Example Let's implement this function step by step: const num = 9119; const squared = num => { const numStr = String(num); ...

Read More

Finding the smallest fitting number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 183 Views

We are required to write a JavaScript function that takes in an array of numbers and returns a number which can exactly divide all the numbers in the array. This is essentially finding the Greatest Common Divisor (GCD) of all numbers. Therefore, let's write the code for this function − Example The code for this will be − const arr = [4, 6, 34, 76, 78, 44, 34, 26, 88, 76, 42]; const dividesAll = el => { const result = []; let num; ...

Read More

Mapping string to Numerals in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 862 Views

We are required to write a JavaScript function that takes in a string. It should print out each number for every corresponding letter in the string. Letter to Number Mapping Each letter corresponds to its position in the alphabet: a = 1 b = 2 c = 3 d = 4 e = 5 ... y = 25 z = 26 Note: The function should remove any special characters and spaces, processing only alphabetic characters. Example Input and Output If the input is: "hello man" Then the output ...

Read More

Writing table of number in array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 168 Views

We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m. Therefore, let's write the code for this function − Example The code for this will be − const num1 = 4; const num2 = 6; const multiples = (num1, num2) => { const res = []; for(let i = num1; i { return Array.from({length: count}, (_, index) => base * (index + 1)); }; console.log(generateTable(7, 5)); console.log(generateTable(3, 8)); [ 7, 14, 21, 28, 35 ] [ 3, 6, 9, 12, 15, 18, 21, 24 ] Using a Simple For Loop Here's another straightforward approach using a basic for loop: function createMultiplicationTable(number, count) { const table = []; for (let i = 1; i

Read More

Chunking arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 201 Views

Chunking arrays in JavaScript means splitting a single array into smaller subarrays of a specified size. This is useful for pagination, data processing, and organizing information into manageable groups. For example, if we have an array of 7 elements and want chunks of size 2, the last chunk will contain only 1 element since 7 is not evenly divisible by 2. Input and Expected Output Given this input array: const arr = [1, 2, 3, 4, 5, 6, 7]; The expected output should be: [[1, 2], [3, 4], [5, 6], [7]] ...

Read More

Fetching odd appearance number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 162 Views

Given an array of integers, we need to find the element that appears an odd number of times. There will always be exactly one such element. We can solve this problem using different approaches. The sorting approach iterates through a sorted array to count occurrences, while XOR provides an elegant mathematical solution. Method 1: Using Sorting This approach sorts the array first, then iterates through it to count consecutive identical elements. const arr = [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]; const findOddSorting = ...

Read More

How can I merge properties of two JavaScript objects dynamically?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 280 Views

To merge properties of two JavaScript objects dynamically, you can use the spread operator ({...object1, ...object2}) or Object.assign(). Both methods create a new object containing properties from multiple source objects. Using Spread Operator (Recommended) The spread operator is the modern and most concise approach: var firstObject = { firstName: 'John', lastName: 'Smith' }; var secondObject = { countryName: 'US' }; var mergedObject = {...firstObject, ...secondObject}; console.log(mergedObject); { firstName: 'John', lastName: 'Smith', countryName: 'US' } Using Object.assign() ...

Read More

How to sort an array of integers correctly in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 428 Views

To sort an array of integers correctly in JavaScript, use the sort() method with a comparison function. Without a comparison function, sort() converts numbers to strings, causing incorrect ordering. The Problem with Default sort() JavaScript's default sort() method converts elements to strings and sorts alphabetically, which doesn't work correctly for numbers: var numbers = [10, 2, 100, 5]; console.log("Default sort:", numbers.sort()); Default sort: [ 10, 100, 2, 5 ] Correct Integer Sorting Use a comparison function that returns the difference between two numbers: var arrayOfIntegers = [67, 45, ...

Read More

Understanding the find() method to search for a specific record in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 530 Views

The find() method searches through an array and returns the first element that matches a specified condition. It's perfect for locating specific records in arrays of objects. Syntax array.find(callback(element, index, array)) Parameters callback - Function to test each element element - Current element being processed index (optional) - Index of current element array (optional) - The array being searched Return Value Returns the first element that satisfies the condition, or undefined if no element is found. Example: Finding a Student Record var students = [ ...

Read More
Showing 101–110 of 5,881 articles
« Prev 1 9 10 11 12 13 589 Next »
Advertisements