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 136 of 534
Sorting array of Number by increasing frequency JavaScript
We are required to write a JavaScript function that takes in an array of numbers that might contain some repeating numbers. The function should sort the array such that the elements that are repeated for the least number of times appears first followed by the elements with increasing frequency. For example − If the input array is − const arr = [1, 1, 2, 2, 2, 3]; Then the sorted array should be − const output = [3, 1, 1, 2, 2, 2]; How It Works The solution ...
Read MoreJavaScript - find distance between items on array
In JavaScript, finding the distance between items in an array involves calculating the difference between each element and all succeeding elements. This creates a matrix of distances that can be useful for various algorithms and data analysis. Suppose we have a sorted (increasing order) array of Numbers like this: const arr = [2, 5, 7, 8, 9]; We need to write a JavaScript function that takes in one such array. The function should construct a new subarray for each element of the input array containing the differences between that element and all succeeding elements. ...
Read MoreFinding Number of Days Between Two Dates JavaScript
Finding the number of days between two dates is a common requirement in JavaScript applications. While you can implement complex date calculations manually, JavaScript's built-in Date object provides a much simpler and more reliable approach. The Simple Approach Using Date Objects The most straightforward method is to convert date strings to Date objects and calculate the difference in milliseconds, then convert to days: const daysBetweenDates = (date1, date2) => { const firstDate = new Date(date1); const secondDate = new Date(date2); ...
Read MoreCapturing JavaScript error in Selenium.
We can capture JavaScript errors in Selenium WebDriver to identify console errors that appear in the browser's Developer Tools. These errors can occur due to functional issues on the page or performance problems caused by excessive logging. JavaScript errors are typically visible in the Console tab of browser Developer Tools and can impact application functionality. Selenium provides logging capabilities to capture and analyze these errors programmatically. Browser Developer Tools - Console Tab ...
Read MoreWait for complex page with JavaScript to load using Selenium.
We can wait for a complex page with JavaScript to load with Selenium. After the page is loaded, we can invoke the Javascript method document.readyState and wait till complete is returned. Understanding document.readyState The document.readyState property returns the loading status of the document. It has three possible values: loading - The document is still loading interactive - Document has finished loading but sub-resources may still be loading complete - The document and all sub-resources have finished loading Syntax JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("return document.readyState").toString().equals("complete"); Method 1: Using document.readyState Check ...
Read MoreUnique intersection of arrays in JavaScript
We are required to write a JavaScript function that takes in two arrays of numbers, let's say arr1 and arr2. The function should find the intersection between the elements of the array. i.e., the elements that appear in both the arrays. The only condition is that if we encountered one element before as intersected, we should not consider it again even if appears again in both the arrays. For example − If the input arrays are − const arr1 = [1, 5, 7, 3, 1]; const arr2 = [1, 7, 3, 1, 6]; ...
Read MoreDecimal to binary conversion using recursion in JavaScript
We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should use recursion to construct a string representing the binary notation of that number. For example − f(4) = '100' f(1000) = '1111101000' f(8) = '1000' How Binary Conversion Works Binary conversion involves repeatedly dividing a number by 2 and collecting the remainders. The remainders, read in reverse order, form the binary representation. Decimal 4 to Binary 4 ÷ 2 ...
Read MoreFinding sum of alternative elements of the array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the only argument. The function should calculate and return the sum of alternative elements of the array. For example − If the input array is − const arr = [1, 2, 3, 4, 5, 6, 7]; Then the output should be − 1 + 3 + 5 + 7 = 16 Method 1: Using for Loop with Index Check This approach uses a for loop and checks if the index is even to ...
Read MoreConverting object to 2-D array in JavaScript
In JavaScript, we often need to convert objects into 2D arrays where each sub-array contains a key-value pair. This is useful for data processing, iteration, or when working with APIs that expect array formats. Suppose we have an object that contains information about the weather of a city: const obj = { city: "New Delhi", maxTemp: 32, minTemp: 21, humidity: 78, aqi: 456, day: 'Tuesday', }; We need to convert this object into a 2D array where ...
Read MoreConverting array of arrays into an object in JavaScript
Converting an array of arrays into an object is a common task in JavaScript when dealing with key-value pair data. This is particularly useful when transforming data from APIs or CSV-like structures. Suppose we have an array of arrays that contains the performance of a cricket player like this: const arr = [ ['Name', 'V Kohli'], ['Matches', 13], ['Runs', 590], ['Highest', 183], ['NO', 3], ['SR', 131.5] ]; We need to write a JavaScript function that takes such ...
Read More