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
Front End Technology Articles
Page 181 of 652
Recursive string parsing into object - JavaScript
Recursive string parsing transforms an array of dot-separated strings into a nested JavaScript object structure. This technique is useful for converting flat configuration paths into hierarchical data structures. Problem Description Given an array of strings following the pattern x.y.x.y..., we need to create a nested object where the last segment becomes a value in an array, and preceding segments form the nested structure. For example, if the input array is: const arr = [ "country.UK.level.1", "country.UK.level.2", "country.US.level.1", "country.UK.level.3" ...
Read MoreCalculating the weight of a string in JavaScript
In JavaScript, calculating the weight of a string means finding the sum of alphabetical positions of all characters. Each letter's weight is its position in the alphabet (a=1, b=2, c=3, etc.). Understanding Character Weight The weight of an English alphabet character is its 1-based index position: 'a' has weight 1 'b' has weight 2 'c' has weight 3 'z' has weight 26 Method 1: Using indexOf() This approach uses a reference string to find each character's position: const str = 'this is a string'; const calculateWeight = (str = '') ...
Read MoreHow to sort date array in JavaScript
Sorting arrays of dates is a common task in JavaScript. This tutorial shows how to sort an array containing date strings in ascending chronological order. Suppose we have an array that contains some dates like this: const arr = [ [ '02/13/2015', 0.096 ], [ '11/15/2013', 0.189 ], [ '05/15/2014', 0.11 ], [ '12/13/2013', 0.1285 ], [ '01/15/2013', 0.12 ], [ '01/15/2014', 0.11 ], [ '02/14/2014', 0.11 ], ...
Read More2 Key keyboard problem in JavaScript
In this problem, we start with a notepad containing one character 'A' and need to reach exactly 'n' characters using only two operations: Copy All and Paste. The goal is to find the minimum number of steps required. Problem Understanding Given a notepad with one 'A', we can perform: Copy All − Copy all characters currently on the notepad Paste − Paste the previously copied characters We need to find the minimum steps to get exactly 'n' A's on the notepad. Example Walkthrough For ...
Read MoreSquaring every digit of a number using split() 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 then square every digit of the number, append them and yield the new number. For example − If the input number is − const num = 12349; Then the output should be − const output = 1491681; because '1' + '4' + '9' + '16' + '81' = 1491681 How It Works The solution uses split('') to convert each digit into an array element, then ...
Read MoreHow do I console.log JavaScript variables as it relates to DOM?
To display variables on console when working with the DOM, you can use console.log() to output element objects, their properties, or values. The document.getElementById() method retrieves DOM elements which can then be logged to examine their properties. Logging DOM Elements When you log a DOM element directly, the browser console displays the element object with all its properties: Console Log DOM Example Hello World ...
Read MoreCalculating time taken to type words in JavaScript
In JavaScript, we can calculate the time taken to type words on a custom keyboard where keys are arranged alphabetically (a-z) instead of the traditional QWERTY layout. Problem Setup We make two key assumptions: The fingertip starts at index 0 (key 'a') Time to move between keys equals the absolute difference of their positions. For example, moving from 'a' (index 0) to 'k' (index 10) takes |0 - 10| = 10 time units Example Walkthrough For the string 'dab', the movements are: 'a' ...
Read MoreAdd property to common items in array and array of objects - JavaScript?
To add properties to objects based on common items in arrays, use the map() method combined with Set for efficient lookups. This approach allows you to conditionally add properties to array objects based on whether their values exist in another array. Example Arrays Let's start with a simple array and an array of objects: const firstname = ['John', 'David', 'Bob']; const studentDetails = [ { firstname: 'Carol', marks: 78 }, ...
Read MoreMap anagrams to one another in JavaScript
One array is an anagram of another if we can rearrange the elements of that array to achieve the other array. For example: [1, 2, 3] and [2, 1, 3] are anagrams of each other. We need to write a JavaScript function that takes two anagram arrays and returns a mapping array. The mapping array contains the index of each element from the first array as it appears in the second array. Problem Example If the two input arrays are: const arr1 = [23, 39, 57, 43, 61]; const arr2 = ...
Read MoreHow to edit values of an object inside an array in a class - JavaScript?
In JavaScript, you can edit values of objects inside an array within a class by using the this keyword. This allows you to reference and modify properties of objects stored in class arrays. Understanding the Structure When working with objects inside arrays in classes, each object can contain its own methods that modify its properties. The key is understanding how this refers to the current object context. Example Here's how to create a class with an array of objects and modify their values: class Employee { constructor() { ...
Read More