Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 467 of 801
Middle of three elements - JavaScript
We are required to write a JavaScript function that takes in three unsorted numbers and returns the middlemost of them using minimum number of comparisons. For example: If the numbers are − 34, 45, 12 Then our function should return the following − 34 Algorithm Explanation The algorithm uses mathematical differences to determine the middle element without explicit sorting. By calculating differences between pairs and checking their signs, we can identify the middle value efficiently. Example Following is the code − const num1 = 34; const ...
Read MoreFilter away object in array with null values JavaScript
When working with arrays of objects, you often need to filter out objects with invalid or empty values. This is common when processing API responses or user data that may contain incomplete records. Let's say we have an array of employee objects, but some have empty strings, null, or undefined values for the name field. We need to filter out these invalid entries. Sample Data Here's our employee data with some invalid entries: let data = [{ "name": "Ramesh Dhiman", "age": 67, "experience": ...
Read MoreFinding tidy numbers - JavaScript
A tidy number is a number whose digits are in non-decreasing order. We are required to write a JavaScript function that takes in a number and checks whether its a tidy number or not. For example: 489 is a tidy number 234557 is also a tidy number 34535 is not a tidy number Understanding Tidy Numbers In a tidy number, each digit should be less than or equal to the next digit when reading from left to right. For instance, in 234789, we have 2 ≤ 3 ≤ 4 ≤ 7 ≤ 8 ≤ ...
Read MoreStrictly increasing sequence JavaScript
Given a sequence of integers as an array, we have to determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. For example: For sequence = [1, 3, 2, 1], the output should be function(sequence) = false. There is no one element in this array that can be removed in order to get a strictly increasing sequence. For sequence = [1, 3, 2], the output should be function(sequence) = true. You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, ...
Read MoreArray of multiples - JavaScript
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. For example − If the numbers are 4 and 6 Then the output should be − const output = [4, 8, 12, 16, 20, 24] Example Following is the code − const num1 = 4; const num2 = 6; const multiples = (num1, num2) => { const res = []; for(let i = num1; i ...
Read MoreCheck if values of two arrays are the same/equal in JavaScript
We have two arrays of numbers, let's say − [2, 4, 6, 7, 1] [4, 1, 7, 6, 2] Assume, we have to write a function that returns a boolean based on the fact whether or not they contain the same elements irrespective of their order. For example − [2, 4, 6, 7, 1] and [4, 1, 7, 6, 2] should yield true because they have the same elements but ordered differently. Method 1: Using includes() and Length Check This approach checks if both arrays have the same length and every element from ...
Read MoreConcatenating variable number of arrays into one - JavaScript
We are required to write a JavaScript function that takes in any number of JavaScript arrays and returns one single array with all the values from input arrays concatenated into it. For example − If the input arrays are − [1, 5], [44, 67, 3], [2, 5], [7], [4], [3, 7], [6] Then the output should be − const output = [1, 5, 44, 67, 3, 2, 5, 7, 4, 3, 7, 6]; Using reduce() and concat() The most straightforward approach uses the reduce() method combined with concat() to merge ...
Read MoreInsert value in the middle of every value inside array JavaScript
In JavaScript, you can insert operation objects between array elements to create alternating patterns. This technique is useful for building mathematical expressions or form builders. Problem Statement We have an array of numbers like this: const numbers = [1, 6, 7, 8, 3, 98]; console.log("Original array:", numbers); Original array: [ 1, 6, 7, 8, 3, 98 ] We need to transform this array into objects with "value" keys, and insert operation objects between them using +, -, *, / alternately. Expected Output Structure The final result should look like ...
Read MoreFinding special array - JavaScript
An array is a special array if: - All the elements at odd indices are odd - All the elements at even indices are even We need to write a JavaScript function that takes an array and checks if it's a special array or not. Understanding the Logic For an array to be special: Index 0 (even): element must be even Index 1 (odd): element must be odd Index 2 (even): element must be even Index 3 (odd): element must be odd The key insight is that arr[i] % 2 should ...
Read MoreHow to move multiple elements to the beginning of the array in JavaScript?
In JavaScript, you can move multiple elements to the beginning of an array by identifying their positions and using array methods like splice() and unshift(). This technique is useful when you need to prioritize certain elements while maintaining the relative order of others. Problem Overview We need to create a function that takes an array and any number of strings as arguments. The function should check if these strings exist in the array and move them to the front while preserving their relative order. Using splice() and unshift() Method This approach removes elements from their current ...
Read More