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
Articles on Trending Technologies
Technical articles with clear explanations and examples
AND product of arrays in JavaScript
We have an array of arrays of boolean values like this: const arr = [[true, false, false], [false, false, false], [false, false, true]]; We are required to write a function that merges this array of arrays into a one-dimensional array by combining the corresponding elements of each subarray using the AND (&&) operator. Let's write the code for this function. We will be using Array.prototype.reduce() function to achieve this. Example The code for this will be: const arr = [[true, false, false], [false, false, false], [false, false, true]]; const andMerge ...
Read MoreSorting Array based on another array JavaScript
Sorting an array based on another array's order is a common JavaScript task. This technique allows you to reorder elements according to a reference array's sequence. Problem Statement Given two arrays, we need to sort the first array based on the order of elements in the second array: const input = ['S-1', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8']; const sortingArray = ["S-1", "S-5", "S-2", "S-6", "S-3", "S-7", "S-4", "S-8"]; console.log("Original array:", input); console.log("Reference order:", sortingArray); Original array: [ 'S-1', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8' ] Reference order: [ 'S-1', ...
Read MoreJavaScript: create an array of JSON objects from linking two arrays
Suppose, we have two arrays like these − const meals = ["breakfast", "lunch", "dinner"]; const ingredients = [ ["eggs", "yogurt", "toast"], ["falafel", "mushrooms", "fries"], ["pasta", "cheese"] ]; We are required to write a JavaScript function that takes in two such arrays and maps the subarrays in the second array to the corresponding strings of the first array. Therefore, the output for the above arrays should look like − { "breakfast" : ["eggs", "yogurt", "toast"], ...
Read MoreContiguous subarray with 0 and 1 in JavaScript
In JavaScript, finding the longest contiguous subarray with equal numbers of 0s and 1s in a binary array is a classic problem that can be solved efficiently using a hash map approach. Problem Statement Given a binary array containing only 0s and 1s, find the length of the longest contiguous subarray that contains an equal number of 0s and 1s. For example, with the input array: const arr = [1, 0, 0, 1, 0, 1, 0, 0]; console.log("Input array:", arr); Input array: [1, 0, 0, 1, 0, 1, 0, 0] ...
Read MoreSending personalised messages to user using JavaScript
We need to write a JavaScript function that sends personalized messages based on whether the user is the owner or a regular user. The function takes two parameters: the user name and the owner name. Problem Statement Create a function that compares the user name with the owner name and returns appropriate greetings: If the user is the owner: return "Hello master" If the user is different from owner: return "Hello" followed by the user's name Solution Here's the implementation using a simple conditional statement: const name = 'arnav'; const owner ...
Read MoreImage Transition with Fading Effect using JavaScript
Image transition means changing the image and replacing one image with another. Users can see the image transition in image sliders or galleries. While developing image sliders, we should focus on the animation for image transition to create an attractive user experience. In this tutorial, we will learn to add fading effects using various approaches to image transitions. Method 1: Adding CSS Class for Fade Effect We can use CSS to add a fading effect to image transitions. The transition property of CSS allows us to add smooth transitions to images. By adding CSS to a class ...
Read MoreFinding smallest number using recursion in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers and returns the smallest number from it using recursion. Let's say the following are our arrays: const arr1 = [-2, -3, -4, -5, -6, -7, -8]; const arr2 = [-2, 5, 3, 0]; Recursive Approach The recursive solution uses a helper function that compares the first element with the rest of the array: const arr1 = [-2, -3, -4, -5, -6, -7, -8]; const arr2 = [-2, 5, 3, 0]; const min = arr => { ...
Read MoreListing all the prime numbers upto a specific number in JavaScript
We are required to write a JavaScript function that takes in a number, say n, and returns an array containing all the prime numbers up to n. For example: If the number n is 24. Then the output should be − const output = [2, 3, 5, 7, 11, 13, 17, 19, 23]; Therefore, let's write the code for this function − Method 1: Using Helper Function for Prime Check This approach uses a helper function to check if a number is prime, then iterates through numbers up to n: ...
Read MoreForming the longest word in JavaScript
We are required to write a JavaScript function that takes in a random English alphabet string, str, as the first argument and an array of strings, arr, as the second argument. The task of our function is to try deleting some characters from the string str and check which longest word can be formed that exists in the array arr as well. Our function should return the longest possible string. If there exists no such string, we should return an empty string. Problem Example For example, if the input to the function is: const ...
Read MoreNumbers and operands to words in JavaScript
We are required to write a JavaScript function that takes in a string of some mathematical operation and return its literal wording. Problem Converting mathematical expressions like "5 - 8" into readable words like "Five Minus Eight" requires mapping numbers and operators to their text equivalents. Approach We'll create two lookup objects: one for operators and another for numbers. Then parse the input string and convert each part using these mappings. Example Following is the code − const str = '5 - 8'; const convertToWords = (str = '') => { ...
Read More