Web Development Articles

Page 170 of 801

Inverting slashes in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 423 Views

When working with strings in JavaScript, you may need to replace forward slashes (/) with backslashes (\) or vice versa. This is common when dealing with file paths or URL manipulation. This article demonstrates how to create a function that takes a string containing forward slashes and converts them to backslashes. Problem Statement We need to write a JavaScript function that: Takes a string that may contain forward slashes (/) Returns a new string with all forward slashes replaced by backslashes (\) Method 1: Using a for Loop Here's a solution using a ...

Read More

Picking out uniques from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 179 Views

Suppose we have an array that contains duplicate elements like this − const arr = [1, 1, 2, 2, 3, 4, 4, 5]; We are required to write a JavaScript function that takes in one such array and returns a new array. The array should only contain the elements that only appear once in the original array. Therefore, let's write the code for this function − Method 1: Using indexOf() and lastIndexOf() This approach compares the first and last occurrence of each element. If they're different, the element appears multiple times: ...

Read More

Merging two arrays in a unique way in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 185 Views

We are required to write a JavaScript function that takes in two arrays and merges the arrays taking elements alternatively from the arrays. For example If the two arrays are − const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3]; Then the output should be − [4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3] Using Index-Based Approach The code for this will be − const arr1 = [4, 3, 2, ...

Read More

Adding up identical elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 153 Views

We are required to write a JavaScript function that takes in an array of numbers and sums all the identical numbers together at one index. Problem Overview When we have an array with duplicate values, we want to combine them into a single occurrence with their sum. The first occurrence of each unique number will contain the total sum of all occurrences. Example Input and Output If the input array is: const arr = [20, 10, 15, 20, 15, 10]; Then the output should be: const output = [40, 20, ...

Read More

Squared concatenation of a Number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 299 Views

We are required to write a JavaScript function that takes in a number and returns a new number in which all the digits of the original number are squared and concatenated. For example: If the number is − 99 Then the output should be − 8181 because 9² is 81 and 9² is 81, so concatenating them gives 8181. Example Let's implement this function step by step: const num = 9119; const squared = num => { const numStr = String(num); ...

Read More

Finding the smallest fitting number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 190 Views

We are required to write a JavaScript function that takes in an array of numbers and returns a number which can exactly divide all the numbers in the array. This is essentially finding the Greatest Common Divisor (GCD) of all numbers. Therefore, let's write the code for this function − Example The code for this will be − const arr = [4, 6, 34, 76, 78, 44, 34, 26, 88, 76, 42]; const dividesAll = el => { const result = []; let num; ...

Read More

Mapping string to Numerals in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 868 Views

We are required to write a JavaScript function that takes in a string. It should print out each number for every corresponding letter in the string. Letter to Number Mapping Each letter corresponds to its position in the alphabet: a = 1 b = 2 c = 3 d = 4 e = 5 ... y = 25 z = 26 Note: The function should remove any special characters and spaces, processing only alphabetic characters. Example Input and Output If the input is: "hello man" Then the output ...

Read More

Writing table of number in array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 177 Views

We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m. Therefore, let's write the code for this function − Example The code for this will be − const num1 = 4; const num2 = 6; const multiples = (num1, num2) => { const res = []; for(let i = num1; i { return Array.from({length: count}, (_, index) => base * (index + 1)); }; console.log(generateTable(7, 5)); console.log(generateTable(3, 8)); [ 7, 14, 21, 28, 35 ] [ 3, 6, 9, 12, 15, 18, 21, 24 ] Using a Simple For Loop Here's another straightforward approach using a basic for loop: function createMultiplicationTable(number, count) { const table = []; for (let i = 1; i

Read More

Chunking arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 209 Views

Chunking arrays in JavaScript means splitting a single array into smaller subarrays of a specified size. This is useful for pagination, data processing, and organizing information into manageable groups. For example, if we have an array of 7 elements and want chunks of size 2, the last chunk will contain only 1 element since 7 is not evenly divisible by 2. Input and Expected Output Given this input array: const arr = [1, 2, 3, 4, 5, 6, 7]; The expected output should be: [[1, 2], [3, 4], [5, 6], [7]] ...

Read More

Fetching odd appearance number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 170 Views

Given an array of integers, we need to find the element that appears an odd number of times. There will always be exactly one such element. We can solve this problem using different approaches. The sorting approach iterates through a sorted array to count occurrences, while XOR provides an elegant mathematical solution. Method 1: Using Sorting This approach sorts the array first, then iterates through it to count consecutive identical elements. const arr = [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]; const findOddSorting = ...

Read More
Showing 1691–1700 of 8,010 articles
« Prev 1 168 169 170 171 172 801 Next »
Advertisements