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 292 of 840
Turning a 2D array into a sparse array of arrays in JavaScript
Suppose, we have a 2-D array like this − const arr = [ [3, 1], [2, 12], [3, 3] ]; We are required to write a JavaScript function that takes in one such array. The function should then create a new 2-D array that contains all elements initialized to undefined other than the element's index present in the input array. Therefore, for the input array: output[3][1] = 1; output[2][12] = 1; output[3][3] = 1; And rest all elements ...
Read MoreGrouping names based on first letter in JavaScript
Suppose, we have an array of names like this: const arr = ["Simon", "Mike", "Jake", "Lara", "Susi", "Blake", "James"]; We are required to write a JavaScript function that takes in one such array. The function should return an array of objects with two properties: letter -> the letter on which the names are grouped names -> an array of names that falls in that group Example The code for this will be: const arr = ["Simon", "Mike", "Jake", "Lara", "Susi", "Blake", ...
Read MoreSplitting an object into an array of objects in JavaScript
Splitting an object into an array of separate objects is useful when you need to transform key-value pairs into individual objects for easier manipulation or iteration. Suppose we have an object like this: const obj = { "name": "John", "age": 30, "city": "New York", "profession": "Developer" }; We need to write a JavaScript function that takes such an object and returns a new array where each key-value pair becomes its own separate object. Using Object.keys() and forEach() const obj = ...
Read MoreAlphanumeric sorting using JavaScript
We have a mixed array that we need to sort by alphabet and then by digit. This requires a custom sorting function that handles both alphabetical and numerical parts correctly. const arr = ['Ab-1', 'Ab-11', 'Ab-12', 'ab-10', 'ab-100', 'ab-101', 'ab2', 'ab-3', 'ab-105']; console.log("Original array:", arr); Original array: [ 'Ab-1', 'Ab-11', 'Ab-12', 'ab-10', 'ab-100', 'ab-101', 'ab2', 'ab-3', 'ab-105' ] The Problem with Standard Sort JavaScript's default string sort doesn't handle numbers correctly. It treats "10" as smaller than "2" because it compares character ...
Read MoreReplacing vowels with their 1-based index in a string in JavaScript
We are required to write a JavaScript function that takes in a string and replaces all occurrences of the vowels in the string with their index in the string (1-based). It means if the second letter of the string is a vowel, it should be replaced by 2. Example Following is the code − const str = 'cancotainsomevowels'; const replaceVowels = (str = '') => { const vowels = 'aeiou'; let res = ''; for(let i = 0; i < str.length; i++){ ...
Read MoreHow to iterate over objects in array and sum a property in JavaScript
Suppose we have an array of objects like this: const arr = [ { duration: 10, any: 'fields' }, { duration: 20, any: 'other fields' }, { duration: 15, any: 'some other fields' } ...
Read MoreSorting parts of array separately in JavaScript
We have an array that contains many objects. We are required to write a function to sort the first half of the array in ascending order and the second half of the array in ascending order as well, but without inter-mixing the entries of halves into one another. Consider this sample array: const arr = [ {id:1, x: 33}, {id:2, x: 22}, {id:3, x: 11}, {id:4, x: 3}, {id:5, x: 2}, {id:6, x: ...
Read MoreEnter a number and write a function that adds the digits together on button click in JavaScript
We are required to write a JavaScript program that provides users an input to fill in a number. And upon filling when the user clicks the button, we should display the sum of all the digits of the number. HTML Structure First, let's create the HTML elements needed for our application: Submit JavaScript Implementation Here's the JavaScript function that calculates the sum of digits: function myFunc() { var num = document.getElementById('digits').value; var tot = 0; ...
Read MoreRetrieving n smallest numbers from an array in their original order in JavaScript
We need to write a JavaScript function that takes an array of numbers and a number n, then returns the n smallest numbers while preserving their original order in the array. Problem Given an array of numbers and a value n, we want to find the n smallest numbers but maintain their relative positions from the original array. The result should not be sorted numerically but should appear in the same order as they existed in the input array. Example Here's how we can solve this problem: const arr = [6, 3, 4, 1, ...
Read MoreGenerate Ranking with combination of strings in JavaScript
We are required to write a JavaScript function that takes in any number of arrays of numbers and generates a frequency map of all possible combinations within each array. The function counts how many times each element and combination appears across all arrays. For example, if we have the following arrays: const a = [23, 45, 21], b = [45, 23], c = [21, 32], d = [23], e = [32], f = [50, 54]; The function should ...
Read More