Object Oriented Programming Articles

Page 55 of 589

Unique intersection of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 361 Views

We are required to write a JavaScript function that takes in two arrays of numbers, let's say arr1 and arr2. The function should find the intersection between the elements of the array. i.e., the elements that appear in both the arrays. The only condition is that if we encountered one element before as intersected, we should not consider it again even if appears again in both the arrays. For example − If the input arrays are − const arr1 = [1, 5, 7, 3, 1]; const arr2 = [1, 7, 3, 1, 6]; ...

Read More

Decimal to binary conversion using recursion in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 511 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should use recursion to construct a string representing the binary notation of that number. For example − f(4) = '100' f(1000) = '1111101000' f(8) = '1000' How Binary Conversion Works Binary conversion involves repeatedly dividing a number by 2 and collecting the remainders. The remainders, read in reverse order, form the binary representation. Decimal 4 to Binary 4 ÷ 2 ...

Read More

Finding sum of alternative elements of the array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 986 Views

We are required to write a JavaScript function that takes in an array of Numbers as the only argument. The function should calculate and return the sum of alternative elements of the array. For example − If the input array is − const arr = [1, 2, 3, 4, 5, 6, 7]; Then the output should be − 1 + 3 + 5 + 7 = 16 Method 1: Using for Loop with Index Check This approach uses a for loop and checks if the index is even to ...

Read More

Converting object to 2-D array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 884 Views

In JavaScript, we often need to convert objects into 2D arrays where each sub-array contains a key-value pair. This is useful for data processing, iteration, or when working with APIs that expect array formats. Suppose we have an object that contains information about the weather of a city: const obj = { city: "New Delhi", maxTemp: 32, minTemp: 21, humidity: 78, aqi: 456, day: 'Tuesday', }; We need to convert this object into a 2D array where ...

Read More

Converting array of arrays into an object in JavaScript

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

Converting an array of arrays into an object is a common task in JavaScript when dealing with key-value pair data. This is particularly useful when transforming data from APIs or CSV-like structures. Suppose we have an array of arrays that contains the performance of a cricket player like this: const arr = [ ['Name', 'V Kohli'], ['Matches', 13], ['Runs', 590], ['Highest', 183], ['NO', 3], ['SR', 131.5] ]; We need to write a JavaScript function that takes such ...

Read More

Trim off '?' from strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 182 Views

We are required to write a JavaScript function that takes in a string as the only argument. The string is likely to contain question marks (?) in the beginning and the end. The function should trim off all these question marks from the beginning and the end keeping everything else in place. For example − If the input string is − const str = '??this is a ? string?'; Then the output should be − 'this is a ? string' Method 1: Using Regular Expressions The simplest approach uses ...

Read More

Insert a character at nth position in string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 655 Views

We are required to write a JavaScript function that takes in a string as the first argument and a number as the second argument and a single character as the third argument, let's call this argument char. The number is guaranteed to be smaller than the length of the array. The function should insert the character char after every n characters in the string and return the newly formed string. For example − If the arguments are − const str = 'NewDelhi'; const n = 3; const char = ' '; Then the ...

Read More

Comparing the performance of recursive and looped factorial function in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 269 Views

We will be writing two JavaScript functions, the job of both the functions will be to take in a number and return its factorial. The first function should make use of a for loop or while loop to compute the factorial. Whereas the second function should compute the factorial using a recursive approach. Lastly, we should compare the times taken by these functions over a large number of iterations. Iterative Factorial Function The iterative approach uses a simple for loop to calculate the factorial: const factorial = (num = 1) => { ...

Read More

Primality test of numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 571 Views

A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. All other natural numbers greater than 1 are called composite numbers. A primality test is an algorithm for determining whether an input number is prime. We are required to write a JavaScript function that takes in a number and checks whether it is a prime or not. Basic Primality Test Algorithm The most efficient approach is to check divisibility up to the square root of the number, skipping even numbers after checking for 2. ...

Read More

Euclidean Algorithm for calculating GCD in JavaScript

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

In mathematics, Euclid's algorithm is a method for computing the greatest common divisor (GCD) of two numbers, the largest number that divides both of them without leaving a remainder. The Euclidean algorithm is based on the principle that the greatest common divisor of two numbers does not change if the larger number is replaced by its difference with the smaller number. For example, 21 is the GCD of 252 and 105 (as 252 = 21 × 12 and 105 = 21 × 5), and the same number 21 is also the GCD of 105 and 252 − 105 ...

Read More
Showing 541–550 of 5,881 articles
« Prev 1 53 54 55 56 57 589 Next »
Advertisements