Object Oriented Programming Articles

Page 17 of 589

Number to alphabets in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 906 Views

We are required to write a JavaScript function that takes in a string of any variable length that represents a number. Our function is supposed to convert the number string to the corresponding letter string. For example − If the number string is − const str = '78956'; Then the output should be − const output = 'ghief'; If the number string is − const str = '12345'; Then the output string should be − const output = 'lcde'; Notice how we ...

Read More

Recursive string parsing into object - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 588 Views

Recursive string parsing transforms an array of dot-separated strings into a nested JavaScript object structure. This technique is useful for converting flat configuration paths into hierarchical data structures. Problem Description Given an array of strings following the pattern x.y.x.y..., we need to create a nested object where the last segment becomes a value in an array, and preceding segments form the nested structure. For example, if the input array is: const arr = [ "country.UK.level.1", "country.UK.level.2", "country.US.level.1", "country.UK.level.3" ...

Read More

Multiply and Sum Two Arrays in JavaScript

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

We are required to write a JavaScript function that takes in two arrays of equal length. The function should multiply the corresponding (by index) values in each, and sum the results. For example: If the input arrays are − const arr1 = [2, 3, 4, 5]; const arr2 = [4, 3, 3, 1]; then the output should be 34, because: (2*4 + 3*3 + 4*3 + 5*1) = 8 + 9 + 12 + 5 = 34 Using for Loop The most straightforward approach uses a for loop to iterate ...

Read More

How to sort date array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 500 Views

Sorting arrays of dates is a common task in JavaScript. This tutorial shows how to sort an array containing date strings in ascending chronological order. Suppose we have an array that contains some dates like this: const arr = [ [ '02/13/2015', 0.096 ], [ '11/15/2013', 0.189 ], [ '05/15/2014', 0.11 ], [ '12/13/2013', 0.1285 ], [ '01/15/2013', 0.12 ], [ '01/15/2014', 0.11 ], [ '02/14/2014', 0.11 ], ...

Read More

Generating Random Prime Number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 674 Views

We are required to write a JavaScript function that takes in two numbers specifying a range. Our function should return a random prime number that falls in that range. Algorithm Overview The solution uses the Sieve of Eratosthenes algorithm to find all prime numbers in the range, then randomly selects one. This approach ensures efficiency for finding multiple primes. Example The code for this will be − const range = [100, 1000]; const getPrimes = (min, max) => { const result = Array(max + 1) ...

Read More

How do I console.log JavaScript variables as it relates to DOM?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 164 Views

To display variables on console when working with the DOM, you can use console.log() to output element objects, their properties, or values. The document.getElementById() method retrieves DOM elements which can then be logged to examine their properties. Logging DOM Elements When you log a DOM element directly, the browser console displays the element object with all its properties: Console Log DOM Example Hello World ...

Read More

How to round up to the nearest N in JavaScript

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

In JavaScript, rounding a number to the nearest multiple of N requires dividing by N, rounding the result, then multiplying back by N. Consider this example: const num = 76; When rounding to different factors: Round to nearest 10: 76 becomes 80 Round to nearest 100: 76 becomes 100 Round to nearest 1000: 76 becomes 0 We need a JavaScript function that takes a number and a rounding factor, returning the rounded result. Syntax const roundOffTo = (num, factor = 1) => { const ...

Read More

How to edit values of an object inside an array in a class - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 304 Views

In JavaScript, you can edit values of objects inside an array within a class by using the this keyword. This allows you to reference and modify properties of objects stored in class arrays. Understanding the Structure When working with objects inside arrays in classes, each object can contain its own methods that modify its properties. The key is understanding how this refers to the current object context. Example Here's how to create a class with an array of objects and modify their values: class Employee { constructor() { ...

Read More

Generating a random number that is divisible by n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 617 Views

We are required to write a JavaScript function that takes in a number as the only argument. The function should then return a random generated number which is always divisible by the number provided by the argument. Approach To generate a random number divisible by n, we generate a random number within a range, divide it by n, round the result, and multiply back by n. This ensures the final number is always a multiple of n. Example The code for this will be: const num = 21; // function that generates random ...

Read More

How to allocate memory to an object whose length is set to 0 - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 131 Views

In JavaScript, you can manipulate array length to clear memory and allocate new slots. When you set an array's length to 0, it clears all elements. Setting length to a larger value allocates empty slots. Understanding Array Length Property The length property controls how many elements an array can hold. Setting it to 0 removes all elements, while setting it to a larger number creates empty slots filled with undefined. var arrayObject = [ "John", "David", "Mike" ]; console.log("Original array:", arrayObject); console.log("Original ...

Read More
Showing 161–170 of 5,881 articles
« Prev 1 15 16 17 18 19 589 Next »
Advertisements