Front End Technology Articles

Page 160 of 652

JavaScript Program to Find Common Elements in Two Sorted Arrays

Ravi Ranjan
Ravi Ranjan
Updated on 15-Mar-2026 682 Views

To find common elements in two sorted arrays using JavaScript, we will be discussing various approaches. Common element refers to element which is present in both the arrays. First, we will try brute force approach and then we will optimize our code to improve the time complexity. In this article we are having two sorted arrays, our task is to find common elements in two sorted arrays. Users must already know about JavaScript arrays, its function and operations on arrays, loops, searching technique and conditional statement. ...

Read More

Calculate Subtraction of diagonals-summations in a two-dimensional matrix using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 296 Views

Suppose we have a square matrix represented by a 2-D array in JavaScript like this: const arr = [ [1, 3, 5], [3, 5, 7], [2, 4, 2] ]; console.log(arr); [ [ 1, 3, 5 ], [ 3, 5, 7 ], [ 2, 4, 2 ] ] We need to write a JavaScript function that calculates the absolute difference between the sum of elements on the two diagonals of the matrix. Understanding the Diagonals In a square matrix, there are two diagonals: ...

Read More

How to validate if an element in an array is repeated? - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 355 Views

We are required to write a JavaScript function that takes in two arguments: An Array, say arr, of literals that may contain some repeating elements. A number, say limit. The function should validate that no element of the array is repeated more than limit number of times. If any element is repeated more than the limit the function should return false, true otherwise. Using Object to Count Occurrences The most efficient approach is to use an object to count each element's occurrences, then check if any count exceeds ...

Read More

Compare and fill arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 295 Views

We are required to write a function that compares two arrays and creates a third array filling that array with all the elements of the second array and filling null for all those elements that are present in the first array but misses out in the second array. For example − If the two arrays are − const arr1 = ['f', 'g', 'h']; const arr2 = ['f', 'h']; Then the output should be − const output = ['f', null, 'h']; Algorithm Approach The solution uses an offset variable to ...

Read More

How to select the middle of an array? - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 690 Views

We are required to write a JavaScript function that takes in an array of numbers and returns the middlemost element(s) of the array. For arrays with odd length, there's one middle element. For arrays with even length, there are two middle elements. For example, if the array is: const arr = [1, 2, 3, 4, 5, 6, 7]; Then the output should be [4] (the middle element at index 3). Using Array Prototype Method We can extend the Array prototype to add a middle() method that returns the middle element(s): ...

Read More

How to find the biggest number in an array around undefined elements? - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 276 Views

We are required to write a JavaScript function that takes in an array that contains some numbers, some strings and some falsy values. Our function should return the biggest Number from the array. For example − If the input array is the following with some undefined values − const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; Then the output should be 65 Solution Approach We'll filter out non-numeric values and find the maximum among valid numbers. The key is to properly identify numeric values while handling ...

Read More

Behavior of + operator in JavaScript to store large numbers?

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

JavaScript's + operator converts strings to numbers, but regular numbers have precision limits. For large integers beyond JavaScript's safe range, use BigInt() to avoid precision loss. The Problem with + Operator for Large Numbers JavaScript numbers use 64-bit floating point, which can safely represent integers up to Number.MAX_SAFE_INTEGER (2^53 - 1). Beyond this limit, the + operator causes precision loss: console.log("JavaScript's safe integer limit:"); console.log(Number.MAX_SAFE_INTEGER); // Small number - works fine var stringValue1 = "100"; console.log("Small number with + operator:"); console.log(+stringValue1); // Large number - precision loss var stringValue2 = "2312123211345545367"; console.log("Large number with ...

Read More

Looping numbers with object values and push output to an array - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 212 Views

In JavaScript, you can create an array from an object by mapping object values to specific positions using Array.from(). This technique is useful when you have sparse data that needs to be converted into a dense array format. Problem Overview Given an object with numeric keys and values, we want to create an array where the object values are placed at positions corresponding to their keys, filling missing positions with a default value. var numberObject = { 2: 90, 6: 98 }; console.log("The original object:"); console.log(numberObject); The original object: { '2': 90, '6': ...

Read More

JavaScript: lexical scoping issue using new keyword constructor while adding inner function?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 180 Views

When using constructor functions with inner functions, a common lexical scoping issue arises where the inner function cannot access the constructor's this context. This happens because inner functions have their own execution context. The Problem Inner functions don't inherit the this binding from their parent constructor, leading to undefined or incorrect values when trying to access constructor properties. function Employee() { this.technologyName = "JavaScript"; function workingTechnology() { // 'this' here refers to global object, not ...

Read More

How to new line string - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 722 Views

In JavaScript, there are several ways to create new lines in strings depending on where the output will be displayed. For HTML content, use tags, while for console output or plain text, use the escape sequence . Using Tag for HTML Display When displaying text in HTML elements, use the tag to create line breaks: New Line in HTML Original Text ...

Read More
Showing 1591–1600 of 6,519 articles
« Prev 1 158 159 160 161 162 652 Next »
Advertisements