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 by AmitDiwan
Page 472 of 840
How do I recursively remove consecutive duplicate elements from an array?
Suppose, we have an array of Number literals that contains some consecutive redundant entries like this: const testArr = [1, 1, 2, 2, 3, 3, 1, 1, 1]; We are supposed to write a function compress that takes in this array and removes all redundant consecutive entries in place. So that the output looks like this: const output = [1, 2, 3, 1]; Let's write the code for this function using recursion to remove consecutive duplicate elements. Recursive Approach The recursive function works by traversing the array and comparing each ...
Read MoreReverse sum array JavaScript
We are required to write a function, say reverseSum() that takes in two arrays of Numbers, let's say first and second and returns a new array that contains: Sum of first element of first array and last element of second array as first element, sum of second element of first array and second last element of second array, and so on. When any of the array runs out of element before the other, we simply push all the remaining elements to the array. Syntax const reverseSum = ...
Read MoreHow to display JavaScript array items one at a time in reverse on button click?
In this tutorial, we'll learn how to display JavaScript array items one at a time in reverse order when a button is clicked. This technique is useful for creating step-by-step displays or interactive presentations. The Array and Button Setup First, let's define our array of names: var listOfNames = [ 'John', 'David', 'Bob' ]; And create a button that will trigger the reverse display: Click The Button To get the Reverse Value How It Works The approach ...
Read MoreFile and FileReader in JavaScript?
The File and FileReader APIs in JavaScript allow web applications to read and process files selected by users. The File object represents file information, while FileReader enables reading file contents asynchronously. File Object Properties When a user selects a file through an input element, you get access to a File object with these key properties: name - The file's name size - File size in bytes type - MIME type of the file lastModified - Last modification timestamp FileReader Methods FileReader provides several methods to read file contents: readAsText() - Reads ...
Read MoreSimplifying nested array JavaScript
Let's say, we have an array of arrays that contains some elements like this − const arr = [3, 5, 7, 2, [4, NaN, null, 4, 8, [3, undefined, 24, null], null, 5, 1], NaN, 45, 2, 1]; Our job is to write a recursive function that takes in this nested array and replaces all the falsy values inside the array (NaN, undefined and null) with 0. Understanding Falsy Values In JavaScript, falsy values include: null undefined NaN ...
Read MoreCreating an associative array in JavaScript?
In JavaScript, there's no true associative array like in other languages. Instead, you use objects or arrays of objects to achieve similar functionality. JavaScript objects act as associative arrays where you can use string keys to access values. What are Associative Arrays? Associative arrays use named keys instead of numeric indexes. In JavaScript, regular arrays have numeric indexes, but objects provide key-value pairs that function like associative arrays. Method 1: Using Objects The most common approach is using plain JavaScript objects: // Creating an associative array using object var customer = { ...
Read MoreInverting signs in array - JavaScript
We are required to write a JavaScript function that takes in an array of positive as well as negative Numbers and changes the positive numbers to corresponding negative numbers and the negative numbers to corresponding positive numbers in place. Let's write the code for this function — Example Following is the code — const arr = [12, 5, 3, -1, 54, -43, -2, 34, -1, 4, -4]; const changeSign = arr => { arr.forEach((el, ind) => { arr[ind] *= -1; ...
Read MoreFlattening multi-dimensional arrays in JavaScript
JavaScript arrays can be nested to create multi-dimensional structures. Flattening converts these nested arrays into a single-level array. The flat() method provides a clean solution for this operation. Syntax array.flat(depth) Parameters depth (optional): The number of nested levels to flatten. Default is 1. Use Infinity to flatten all levels. Basic Example Array Flattening Demo Flattening Multi-dimensional Arrays ...
Read MoreCheck whether a series of operations yields a given number with JavaScript Recursion
By starting from the number 1 and repeatedly either adding 5 or multiplying by 3, an infinite amount of new numbers can be produced. We are required to write a function that, given a number, tries to find a sequence of such additions and multiplications that produce that number. And returns a boolean based on the fact whether or not there exists any such sequence. For example, the number 13 could be reached by first multiplying by 3 and then adding 5 twice (1 × 3 = 3, 3 + 5 = 8, 8 + 5 = 13), so ...
Read MorePronic numbers in JavaScript
A Pronic number is a number which is the product of two consecutive integers, that is, a number of the form n(n + 1). For example, 6 is a Pronic number because 6 = 2 × 3, and 12 is Pronic because 12 = 3 × 4. We are required to write a JavaScript function that takes in a number and returns true if it is a Pronic number otherwise returns false. Understanding Pronic Numbers The first few Pronic numbers are: 0, 2, 6, 12, 20, 30, 42, 56, 72, 90... ...
Read More