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 305 of 840
Arranging lexicographically and removing whitespaces in JavaScript
We need to write a JavaScript function that takes a string containing alphabets and whitespaces, then returns a new string with characters arranged in case-insensitive alphabetical order while removing whitespace and punctuation. Problem Statement Our function should iterate over the input string and concatenate characters into a new string following "case-insensitively-alphabetical-order-of-appearance" order. Whitespace and punctuation are simply removed. For example: Input: const str = 'some simple letter combination!'; Expected Output: abceeeeiiillmmmnnoooprssttt Solution Using Nested Loops The approach uses nested loops to iterate through each letter of the alphabet (a-z) ...
Read MoreSorting Array without using sort() in JavaScript
Sorting arrays without using the built-in sort() method is a common programming challenge that helps understand sorting algorithms. JavaScript provides several approaches to implement custom sorting logic. Using Array.reduce() for Insertion Sort The reduce() method can implement insertion sort by building a sorted array incrementally: const arr = [4, 56, 5, 3, 34, 37, 89, 57, 98]; const sortWithReduce = arr => { return arr.reduce((acc, val) => { let ind = 0; while(ind < acc.length ...
Read MoreHow to find a value is present in binary tree or not in JavaScript ?
We are required to write a JavaScript function on the prototype object of a BinarySearchTree data type that takes in a value and finds whether or not that value is contained in the BST. Binary Search Tree Structure A Binary Search Tree (BST) is a tree data structure where each node has at most two children. The left child contains values smaller than the parent, and the right child contains values greater than the parent. 50 30 ...
Read MoreAlternatingly combining array elements in JavaScript
We are required to write a JavaScript function that takes in any number of arrays of literals as input. Our function should prepare a new array that contains elements picked alternatingly from all the input arrays. For example, if the input to the function is − Problem Input const arr1 = [1, 2, 3, 4]; const arr2 = [11, 12, 13, 14]; const arr3 = ['a', 'b', 'c']; Expected Output const output = [1, 11, 'a', 2, 12, 'b', 3, 13, 'c', 4, 14]; The function should take ...
Read MoreHow is Ajax different from JavaScript Libraries and Run Time Environments?
This article explores AJAX (Asynchronous JavaScript and XML) and clarifies how it differs from JavaScript libraries and runtime environments in web development. AJAX Introduction and History Ajax, short for Asynchronous JavaScript and XML, is a technique for creating dynamic and interactive web applications. It was first introduced in the early 2000s and has since become a staple of modern web development. The key feature of Ajax is its ability to update parts of a web page without requiring a full page reload. This is achieved by using JavaScript to send and receive data from a server asynchronously, ...
Read MoreFinding the smallest fitting number in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns a number which can exactly divide all the numbers in the array. This is essentially finding the Greatest Common Divisor (GCD) of all numbers. Therefore, let's write the code for this function − Example The code for this will be − const arr = [4, 6, 34, 76, 78, 44, 34, 26, 88, 76, 42]; const dividesAll = el => { const result = []; let num; ...
Read MoreGenerating a random number that is divisible by n in JavaScript
We are required to write a JavaScript function that takes in a number as the only argument. The function should then return a random generated number which is always divisible by the number provided by the argument. Approach To generate a random number divisible by n, we generate a random number within a range, divide it by n, round the result, and multiply back by n. This ensures the final number is always a multiple of n. Example The code for this will be: const num = 21; // function that generates random ...
Read MoreTotal possible ways of making sum of odd even indices elements equal in array in nJavaScript
We need to write a JavaScript function that counts how many elements can be removed from an array such that after removal, the sum of elements at odd indices equals the sum of elements at even indices. The approach involves calculating cumulative sums for odd and even positioned elements, then checking each removal scenario mathematically without actually removing elements. Problem Understanding Given an array, we want to find how many single element removals result in equal odd and even index sums in the remaining array. For example, with array [2, 6, 4, 2]: Remove index ...
Read MoreHow to accept all pending connection requests on LinkedIn using JavaScript?
To accept all pending connection requests on LinkedIn using JavaScript, you would need to use the LinkedIn API and an automation tool. The script would need to navigate to the connection request page and loop through each request, clicking the accept button for each one. This is a very common issue for people who are moderately to highly active on LinkedIn. They get many connection requests every day and must manually click on accept against each request to actually accept them. You can, however make use of JavaScript and the window console to automate this whole process and ...
Read MoreFinding word starting with specific letter in JavaScript
Finding words that start with a specific letter is a common requirement in JavaScript applications. This guide shows different approaches to locate the first array element beginning with a specified character. Problem Statement We need to write a JavaScript function that takes an array of strings and a character, then returns the index of the first string starting with that character. Using substring() Method The substring() method extracts the first character for comparison: const names = ['Naman', 'Kartik', 'Anmol', 'Rajat', 'Keshav', 'Harsh', 'Suresh', 'Rahul']; const firstIndexOf = (arr = [], char = '') ...
Read More