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
Javascript Articles
Page 106 of 534
Finding product of Number digits in JavaScript
We are required to write a JavaScript program that takes in a number and finds the product of all of its digits. Input Output Scenarios Here are examples of finding the product of number digits: Input = 12345 Output = 120 (1 × 2 × 3 × 4 × 5 = 120) We can also work with string numbers and convert them to integers: Input = "12345" Output = 120 Using Math.floor() and Modulo Operator The Math.floor() function returns the largest integer less than or equal to a given ...
Read MoreIf the element repeats, remove all its instances from array in JavaScript
When working with arrays in JavaScript, sometimes you need to remove all instances of elements that appear more than once, keeping only elements that appear exactly once. This is different from typical deduplication where you keep one instance of each duplicate. For example, if the input is: const arr = [763, 55, 43, 22, 32, 43, 763, 43]; console.log("Original array:", arr); Original array: [ 763, 55, 43, 22, 32, 43, 763, 43 ] The output should be: [ 55, 22, 32 ] Notice that 763 and 43 are ...
Read MoreArray flattening using loops and recursion in JavaScript
Array flattening converts nested arrays into a single-level array. JavaScript provides multiple approaches including loops, recursion, and built-in methods. Problem Overview Given a nested array with mixed data types including falsy values, we need to flatten it completely: const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], [6]]; console.log("Input:", arr); Input: [ [ 1, 2, 3 ], [ 4, 5, [ 5, false, 6, [Array] ] ], [ 6 ] ] Expected output should be a flat array preserving all values including false and null: ...
Read MoreUsing recursion to remove consecutive duplicate entries from an array in JavaScript
We are supposed to write a function that takes in an array of number/string literals. The function should remove all the redundant consecutive elements of the array without using extra memory space. For example, if the input array is − const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1]; Then the output should be − const output = [17, 12, 354, 1]; Therefore, let's write the code for this function − How the Algorithm Works The recursive function works by checking each element against its ...
Read MoreDetecting the largest element in an array of Numbers (nested) in JavaScript
We need to find the largest number in a nested array of any depth. This problem requires recursion to traverse through all nested levels and compare each number to find the maximum value. For example, if we have this nested array: const arr = [ 34, 65, 67, [ 43, 76, 87, 23, 56, 7, [ 54, 7, 87, 23, 79, 994, 2 ], ...
Read MorePicking the largest elements from multidimensional array in JavaScript
Given a multidimensional array, we need to pick the largest elements from each subarray in JavaScript. The multidimensional arrays are arrays inside an array. Whenever there are arrays inside the array, it works as a multidimensional array. Following is a one-dimensional array: const arr = ["Welcome", "to", "tutorials", "point"]; const numbers = [1, 5, 12, 67, 99]; This is how a multidimensional array looks like: const array = [["Mike tyson", "Vijay"], ["ananya", "charmee"], ["Lion", "Tiger"]]; This is how we can access elements from a multidimensional array: ...
Read MoreGreatest element in a Multi-Dimensional Array in JavaScript
We have to write a simple function in JavaScript that takes in an array of Numbers (nested to any level) and return the greatest number present in the array. Problem Example If the input array is: const arr = [ 34, 65, 67, [ 43, 76, 87, 23, 56, 7, [ 54, 7, 87, 23, 79, 994, 2 ], ...
Read MoreRemoving duplicates and inserting empty strings in JavaScript
We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end. Problem Description If we find 4 duplicate values we have to remove them all and insert four empty strings at the end. This maintains the original array length while eliminating duplicates. How It Works The algorithm uses reduce() to iterate through the array. For each element, it checks if the current index matches the last occurrence using lastIndexOf(). If yes, it's the final occurrence and gets added to the ...
Read MoreFilter array based on another array in JavaScript
In this article, we are going to learn how to filter an array based on another array in JavaScript. An Array in JavaScript is used to store different elements. These elements are stored at contiguous memory locations. By using index numbers, we can access any or each data element present in the array. Index numbers start from 0. Syntax Following is the syntax of the array in JavaScript – const array_name = [item1, item2, ...]; The following is a simple declaration of array in JavaScript: const colors = ['Blue', 'Limegreen', 'Orange', ...
Read MoreConstructing a nested JSON object in JavaScript
In JavaScript, we often need to construct nested objects from structured data. This article demonstrates how to build a nested JSON object from a string containing paired characters. Problem Statement Given a string with characters in pairs, we need to construct a nested object where each pair becomes a code property, and each level has a sub-object for the next pair. Input string: const str = "AABBCCDDEE"; Expected output structure: const obj = { code: "AA", sub: { ...
Read More