Javascript Articles

Page 131 of 534

Combine array of objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 409 Views

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 More

Remove all occurrences of a multiple occurring element in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 884 Views

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 More

String to binary in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 7K+ Views

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 More

Implementing Heap Sort using vanilla JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 290 Views

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 More

Prefix calculator using stack in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

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 More

Expressive words problem case in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 261 Views

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 More

Possible combinations and convert into alphabet algorithm in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 268 Views

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 More

Program to make vowels in string uppercase and change letters to next letter in alphabet (i.e. z->a) in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 559 Views

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 More

How to store two arrays as a keyvalue pair in one object in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 774 Views

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 More

Finding the longest "uncommon" sequence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 211 Views

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
Showing 1301–1310 of 5,340 articles
« Prev 1 129 130 131 132 133 534 Next »
Advertisements