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
Web Development Articles
Page 213 of 801
Generating desired pairs within a range using JavaScript
We are required to write a JavaScript function that takes in a number n. Our function should generate an array containing the pairs of integers [a, b] that satisfy the following conditions: 0
Read MoreFinding the smallest value in a JSON object in JavaScript
Finding the smallest value in a JSON object is a common task in JavaScript. This involves traversing through an object's properties and comparing their numeric values to determine the minimum. When working with objects that contain numeric values, we need to iterate through all properties and find the one with the smallest value. JavaScript provides several approaches to accomplish this efficiently. Example Here's how to find the smallest value using the reduce() method: const obj = { "a": 4, "b": 2, "c": 5, ...
Read MoreIs the digit divisible by the previous digit of the number in JavaScript
Problem We need to write a JavaScript function that takes a number and checks if each digit is divisible by the digit on its left. The function returns an array of booleans, where the first element is always false since there's no digit before the first one. Example Let's check the number 73312 digit by digit: const num = 73312; const divisibleByPrevious = (n = 1) => { const str = n.toString(); const arr = [false]; for(let i = 1; ...
Read MoreHow to turn a JSON object into a JavaScript array in JavaScript ?
Converting a JSON object with numeric string keys into a JavaScript array is a common operation. This is particularly useful when dealing with API responses or data structures that use indexed properties. The Problem Consider this JSON object where index keys are mapped to string values: const obj = { "0": "Rakesh", "1": "Dinesh", "2": "Mohit", "3": "Rajan", "4": "Ashish" }; console.log(typeof obj); // "object" object Method 1: Using Object.keys() and forEach() This approach iterates through object keys and maps ...
Read MoreSorting and find sum of differences for an array using JavaScript
Problem We need to write a JavaScript function that takes an array of integers, sorts them in descending order, and then calculates the sum of differences between consecutive pairs. For example, if the array is: [6, 2, 15] After sorting in descending order: [15, 6, 2] The sum of differences would be: (15 - 6) + (6 - 2) = 9 + 4 = 13 Solution Here's the implementation that sorts the array and calculates the sum of consecutive differences: const arr = [6, 2, 15]; ...
Read MoreHow to form a two-dimensional array with given width (columns) and height (rows) in JavaScript ?
We are required to write a JavaScript function that takes in three arguments: height --> no. of rows of the array width --> no. of columns of the array val --> initial value of each element of the array Then the function should return the new array formed based on these criteria. Method 1: Using Array.apply() with map() This approach creates arrays using Array.apply() and fills them with map(): const rows = 4, cols = 5, val = 'Example'; const fillArray = (width, height, value) => { ...
Read MoreChanging second half of string number digits to zero using JavaScript
Problem We are required to write a JavaScript function that takes in a string number as the only argument. Our function should return the input number with the second half of digits changed to 0. In cases where the number has an odd number of digits, the middle digit onwards should be changed to 0. For example: 938473 → 938000 How It Works The algorithm works by calculating the midpoint of the string. For even-length strings, exactly half the digits are preserved. For odd-length strings, the middle digit becomes part of ...
Read MoreCalculating the LCM of multiple numbers in JavaScript
We are required to write a JavaScript function that takes in an array of numbers of any length and returns their LCM (Least Common Multiple). We will approach this problem in parts: Part 1 − We will create a helper function to calculate the Greatest Common Divisor (GCD) of two numbers Part 2 − Then using Part 1 helper function we will create another helper function to calculate the Least Common Multiple (LCM) of two numbers. Part 3 − Finally, using Part 2 helper function we will create a function that loops over the array and ...
Read MoreAccumulating array elements to form new array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a number, num, (num ≤ length of array) as the second argument. Our function should add up each contiguous subarray of length num of the array arr to form corresponding elements of new array and finally return that new array. Problem Example For example, if the input to the function is: const arr = [1, 2, 3, 4, 5, 6]; const num = 2; Then the output should be: [3, ...
Read MoreSorting an array by date in JavaScript
Sorting an array by date in JavaScript is a common task when working with arrays of objects containing date information. This article shows how to sort objects by their date properties using JavaScript's built-in sorting methods. The Problem Suppose we have an array of objects like this: const arr = [ {id: 1, date: 'Mar 12 2012 10:00:00 AM'}, {id: 2, date: 'Mar 8 2012 08:00:00 AM'} ]; We need to sort this array according to the date property of each object, either newest first ...
Read More