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 131 of 534
Combine array of objects in JavaScript
Suppose, we have an array of objects that contains data about some students like this: const arr = [{ name: 'A', idNo: 1, marks: { math: 98, sci: 97, eng: 89 } }, { name: 'B', idNo: 2, marks: { ...
Read MoreRemove all occurrences of a multiple occurring element in an array in JavaScript
We are required to write a JavaScript function that takes in an array of literal values. The array might contain some repeating values. Our function should remove all the values from the array that are repeating. We are required to remove all instances of all such elements. Problem Understanding When an element appears multiple times in an array, we want to remove ALL occurrences of that element, not just the duplicates. For example, if 2 appears twice, we remove both instances. Using filter() with indexOf() and lastIndexOf() The most efficient approach uses filter() to keep only ...
Read MoreString to binary in JavaScript
Converting strings to binary representation is a common task in JavaScript programming. This involves converting each character to its ASCII code and then to binary format. For example, if we have: const str = 'Hello World'; The binary output should be: 1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100 How It Works The conversion process involves three steps: Split the string into individual characters Get the ASCII code of each character using charCodeAt() ...
Read MoreImplementing Heap Sort using vanilla JavaScript
Heap Sort is a comparison-based sorting algorithm that can be thought of as an improved selection sort. Like selection sort, it divides input into sorted and unsorted regions, but uses a binary heap data structure to efficiently find the maximum (or minimum) element. How Heap Sort Works Heap Sort works in two main phases: Build Max Heap: Convert the array into a max heap where parent nodes are larger than their children Extract Elements: Repeatedly remove the maximum element (root) and restore the heap property Implementation Here's a complete implementation of Heap Sort ...
Read MorePrefix calculator using stack in JavaScript
A Reverse Polish Notation (RPN) calculator processes operators after their operands, using a stack data structure. This implementation evaluates mathematical expressions efficiently without parentheses. How RPN Works In RPN, operators follow their operands. For example, "3 4 +" means "3 + 4". The algorithm uses a stack: Push operands onto the stack When encountering an operator, pop two operands, perform the operation, and push the result back The final stack contains the result Step-by-Step Process Consider the input array: const arr = [1, 5, '+', 6, 3, '-', '/', 7, '*']; ...
Read MoreExpressive words problem case in JavaScript
Sometimes people repeat letters to represent extra feeling, such as "hello" → "heeellooo", "hi" → "hiiii". In these strings like "heeellooo", we have groups of adjacent letters that are all the same: "h", "eee", "ll", "ooo". For some given string S, a query word is stretchy if it can be made to be equal to S by any number of applications of the following extension operation: choose a group consisting of characters c, and add some number of characters c to the group so that the size of the group is 3 or more. For example, starting with ...
Read MorePossible combinations and convert into alphabet algorithm in JavaScript
In JavaScript, we can solve the alphabet decoding problem where each letter maps to a number (a=1, b=2, ..., z=26). Given an encoded message, we need to count how many different ways it can be decoded back into letters. For example, the message '111' can be decoded as 'aaa' (1-1-1), 'ka' (11-1), or 'ak' (1-11), giving us 3 possible combinations. Understanding the Problem The challenge is that some digit sequences can be interpreted in multiple ways: Single digits 1-9 can be decoded as letters a-i Two-digit combinations 10-26 can be decoded as letters j-z We ...
Read MoreProgram to make vowels in string uppercase and change letters to next letter in alphabet (i.e. z->a) in JavaScript
We need to write a JavaScript function that takes a string and performs two transformations: convert vowels to uppercase and shift each letter to the next letter in the alphabet (with 'z' wrapping to 'a'). The function should construct a new string based on the input string where all vowels are uppercased and each alphabet character moves to the next letter in sequence. Problem Understanding For example, if the input string is: const str = 'newString'; The expected output should be: const output = 'oExSusIoh'; Here's how the transformation ...
Read MoreHow to store two arrays as a keyvalue pair in one object in JavaScript?
Suppose, we have two arrays of literals of same length like these − const arr1 = ['firstName', 'lastName', 'age', 'address', 'isEmployed']; const arr2 = ['Rahul', 'Sharma', 23, 'Tilak Nagar', false]; We are required to write a JavaScript function that takes in two such arrays. The function should construct an object mapping the elements of the second array to the corresponding elements of the first array. Method 1: Using Array.reduce() We will use the Array.prototype.reduce() method to iterate over the arrays, building the object. const arr1 = ['firstName', 'lastName', 'age', 'address', 'isEmployed']; ...
Read MoreFinding the longest "uncommon" sequence in JavaScript
We are required to write a JavaScript function that takes in an array of strings. The function should find the longest uncommon subsequence among the strings of the array. By longest uncommon subsequence we mean the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings. Our function should return the length of this longest uncommon subsequence. For example: If the input array is − const arr = ["aba", "cdc", "eae"]; Then the output should be 3. Understanding the Problem The key ...
Read More