Object Oriented Programming Articles

Page 27 of 589

Pushing positives and negatives to separate arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 440 Views

We are required to write a function that takes in an array and returns an object with two arrays positive and negative. They both should be containing all positive and negative items respectively from the array. We will be using the Array.prototype.reduce() method to pick desired elements and put them into an object of two arrays. Using Array.reduce() Method The code for this will be − const arr = [97, -108, 13, -12, 133, -887, 32, -15, 33, -77]; const splitArray = (arr) => { return arr.reduce((acc, val) => { ...

Read More

Construct an identity matrix of order n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 956 Views

An identity matrix is a square matrix where all diagonal elements are 1 and all other elements are 0. This type of matrix is fundamental in linear algebra and has the property that when multiplied with any matrix, it returns the original matrix unchanged. What is an Identity Matrix? An identity matrix of order n is an n × n square matrix where: All diagonal elements (where row index equals column index) are 1 All other elements are 0 For example, an identity matrix of order 3 will be: [ [1, ...

Read More

Sum all duplicate values in array in JavaScript

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

We need to write a JavaScript function that takes an array of numbers with duplicate entries and sums all duplicate values. Each unique number appears once in the result, multiplied by its frequency count. Problem Understanding For array [1, 3, 1, 3, 5, 7, 5, 3, 4]: 1 appears 2 times → 1 × 2 = 2 3 appears 3 times → 3 × 3 = 9 5 appears 2 times → 5 × 2 = 10 7 appears 1 time → 7 × 1 = 7 4 appears 1 time → 4 × 1 = 4 ...

Read More

Finding deviations in two Number arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 171 Views

We are required to write a JavaScript function that takes in two number arrays and returns the elements that are not common to both arrays (symmetric difference). For example, if the two arrays are: const arr1 = [2, 4, 2, 4, 6, 4, 3]; const arr2 = [4, 2, 5, 12, 4, 1, 3, 34]; Then the output should be: [6, 5, 12, 1, 34] Method 1: Using indexOf() Method This approach uses nested loops to check if elements exist in the other array: const arr1 = [2, ...

Read More

Excluding extreme elements from average calculation in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 264 Views

We need to write a JavaScript function that calculates the average of array elements while excluding the smallest and largest values. This is useful when you want to remove outliers from statistical calculations. Problem Statement Given an array of numbers, calculate the average excluding the minimum and maximum values. This helps eliminate extreme values that might skew the results. Example Let's implement a function that finds the excluded average: const arr = [5, 3, 5, 6, 12, 5, 65, 3, 2]; const findExcludedAverage = arr => { const creds ...

Read More

Check for perfect square in JavaScript

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

We are required to write a JavaScript function that takes in a number and returns a boolean based on the fact whether or not the number is a perfect square. A perfect square is a number that can be expressed as the product of an integer with itself. For example, 16 is a perfect square because 4 × 4 = 16. Examples of Perfect Square Numbers 144 (12²), 196 (14²), 121 (11²), 81 (9²), 484 (22²) Method 1: Using Math.sqrt() The most straightforward approach is to find the square root and check if ...

Read More

Removing first k characters from string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 206 Views

We are required to write a JavaScript function that takes in a string and a number, say k and returns another string with first k characters removed from the string. For example: If the original string is − const str = "this is a string" and, n = 4 then the output should be − const output = " is a string" Method 1: Using substr() Method The substr() method extracts characters from a string starting at a specified position. const str = 'this ...

Read More

Prime numbers within a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 756 Views

We are required to write a JavaScript function that takes in two numbers, say, a and b and returns the total number of prime numbers between a and b (including a and b, if they are prime). For example: If a = 21, and b = 38. The prime numbers between them are 23, 29, 31, 37 And their count is 4 Our function should return 4 Understanding Prime Numbers A prime number is a natural number greater than 1 that has ...

Read More

ASCII sum difference of strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 686 Views

ASCII (American Standard Code for Information Interchange) is a 7-bit character encoding standard where every character has a unique decimal code. We can calculate the ASCII sum of strings and find their difference in JavaScript. Understanding ASCII Codes Each character has a specific ASCII value. For example, 'A' = 65, 'a' = 97, '0' = 48, and space = 32. We can get these values using JavaScript's charCodeAt() method. Getting ASCII Values console.log('A'.charCodeAt(0)); // 65 console.log('a'.charCodeAt(0)); // 97 console.log('0'.charCodeAt(0)); // 48 console.log(' '.charCodeAt(0)); ...

Read More

Object to Map conversion in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 236 Views

In JavaScript, you can convert an object to a Map using several approaches. Maps offer advantages over objects like guaranteed key insertion order and the ability to use any data type as keys. Suppose we have an object like this: const obj = { name: "Jai", age: 32, occupation: "Software Engineer", address: "Dhindosh, Maharashtra", salary: "146000" }; We need to convert this object into a Map while preserving all key-value pairs. Using Object.entries() (Recommended) The most concise approach uses Object.entries() with the Map ...

Read More
Showing 261–270 of 5,881 articles
« Prev 1 25 26 27 28 29 589 Next »
Advertisements