Javascript Articles

Page 100 of 534

Removing a specific substring from a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 464 Views

We are given a main string and a substring, our job is to create a function, let's say removeString() that takes in these two arguments and returns a version of the main string which is free of the substring. Here, we need to remove the separator from a string, for example: this-is-a-string Using split() and join() Method The most common approach is to split the string by the substring and then join the parts back together: const removeString = (string, separator) => { // we split the string ...

Read More

Checking for the Gapful numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 144 Views

A gapful number is a special type of number that meets specific criteria. Understanding gapful numbers can be useful in mathematical programming and number theory applications. What is a Gapful Number? A number is considered gapful when: It has at least three digits, and It is exactly divisible by the number formed by combining its first and last digits Examples 1053 is a gapful number because it has 4 digits and is divisible by 13 (first digit 1 + last digit 3). 135 is ...

Read More

Natural Sort in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 914 Views

Natural sort in JavaScript refers to sorting arrays containing mixed data types (numbers and strings) in a way that numbers come first in ascending order, followed by strings in alphabetical order. Problem Statement When sorting mixed arrays with the default sort() method, JavaScript converts everything to strings, leading to incorrect ordering. We need a custom sorting function that handles numbers and strings separately. Example Input and Expected Output Let's say this is our array: const arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9]; console.log("Original array:", arr); Original array: ...

Read More

Building frequency map of all the elements in an array JavaScript

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

Building a frequency map (also called a frequency counter) is a common programming task that helps count how many times each element appears in an array. This technique is useful for data analysis, finding duplicates, or solving algorithmic problems. A frequency map returns an object where each unique element from the array becomes a key, and its corresponding value represents how many times that element appears. Basic Approach Using forEach() The most straightforward method is to iterate through the array and build an object that tracks the count of each element: const arr = [2, ...

Read More

Zig-Zag pattern in strings in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 703 Views

We need to write a function that reads a string and converts the odd indexed characters in the string to upperCase and the even ones to lowerCase and returns a new string. Understanding the Pattern A zig-zag pattern alternates between lowercase and uppercase characters based on their index position. Even indices (0, 2, 4...) become lowercase, while odd indices (1, 3, 5...) become uppercase. Example Implementation const text = 'Hello world, it is so nice to be alive.'; const changeCase = (str) => { const newStr = str ...

Read More

Fetching object keys using recursion in JavaScript

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

When working with nested objects in JavaScript, you often need to search for specific keys at any level of nesting. Recursion provides an elegant solution for traversing these complex data structures. The Problem Consider a nested object structure where we need to find all values for a specific key across all levels: const people = { Ram: { fullName: 'Ram Kumar', details: { age: ...

Read More

How to remove some items from array when there is repetition in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 146 Views

In JavaScript, you may need to remove items from an array that appear in multiples of three (triplets). This is useful when you want to keep only the remaining elements after removing complete sets of triplets. Understanding the Problem The goal is to remove triplets (groups of 3 identical elements) from an array and keep only the remaining elements. For example, if an array has 5 occurrences of the number 1, we remove 3 of them (one triplet) and keep 2. Solution Implementation const arr1 = [1, 1, 1, 3, 3, 5]; const arr2 = ...

Read More

Converting array of Numbers to cumulative sum array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 636 Views

We have an array of numbers like this: const arr = [1, 1, 5, 2, -4, 6, 10]; We are required to write a function that returns a new array, of the same size but with each element being the sum of all elements until that point (cumulative sum). Therefore, the output should look like: const output = [1, 2, 7, 9, 5, 11, 21]; Let's explore different approaches to create a cumulative sum array. Using forEach() Method We can iterate through the array and build the cumulative sum by adding each element ...

Read More

Group by element in array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 478 Views

Grouping array elements by a specific property is a common JavaScript task. This tutorial shows how to group an array of objects by their uuid property into separate sub-arrays. Problem Statement Given an array of objects with similar properties, we need to group them by a specific key and return an array of arrays. const arr = [ {"name": "toto", "uuid": 1111}, {"name": "tata", "uuid": 2222}, {"name": "titi", "uuid": 1111} ]; console.log("Original array:", arr); Original array: [ { name: ...

Read More

How to convert array of decimal strings to array of integer strings without decimal in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 745 Views

We are required to write a JavaScript function that takes in an array of decimal strings. The function should return an array of strings of integers obtained by flooring the original corresponding decimal values of the array. For example, If the input array is − const input = ["1.00", "-2.5", "5.33333", "8.984563"]; Then the output should be − const output = ["1", "-2", "5", "8"]; Method 1: Using parseInt() The parseInt() function parses a string and returns an integer. It automatically truncates decimal parts. const input = ["1.00", ...

Read More
Showing 991–1000 of 5,340 articles
« Prev 1 98 99 100 101 102 534 Next »
Advertisements