Web Development Articles

Page 198 of 801

Checking the equality of array elements (sequence dependent) in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 408 Views

In this article, we'll learn how to check the equality of array elements in a sequence-dependent manner. This means comparing arrays element by element at the same index positions to find matching values or count similarities. Input-Output Scenario Let's look at how sequence-dependent comparison works with two arrays: Array1 = [2, 5, 6, 7, 9, 0]; Array2 = [3, 5, 0, 8, 9, 4]; Output = [5, 9] In the above example, elements 5 and 9 appear at the same index positions (index 1 and 4) in both arrays, making them sequence-dependent matches. ...

Read More

Replacing vowels with their 1-based index in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 670 Views

We are required to write a JavaScript function that takes in a string and replaces all occurrences of the vowels in the string with their index in the string (1-based). It means if the second letter of the string is a vowel, it should be replaced by 2. Example Following is the code − const str = 'cancotainsomevowels'; const replaceVowels = (str = '') => { const vowels = 'aeiou'; let res = ''; for(let i = 0; i < str.length; i++){ ...

Read More

Grouping array of array on the basis of elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 266 Views

When working with arrays of arrays in JavaScript, you might need to group elements based on specific criteria. This article demonstrates how to group the second elements of subarrays that share the same first element. Problem Statement Suppose we have an array of arrays where each subarray contains exactly two elements: const arr = [[1, 45], [1, 34], [1, 49], [2, 34], [4, 78], [2, 67], [4, 65]]; console.log("Input array:", arr); Input array: [ [ 1, 45 ], [ 1, 34 ], [ 1, 49 ], [ 2, 34 ], [ 4, 78 ...

Read More

Finding a number, when multiplied with input number yields input number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 198 Views

We need to write a JavaScript function that takes a positive integer n and a positive integer p, then finds a value k such that the sum of digits raised to successive powers equals k * n. Problem Statement Given a number n with digits a, b, c, d... and a power p, we want to find integer k where: (a^p + b^(p+1) + c^(p+2) + d^(p+3) + ...) = n * k If such k exists, return k; otherwise return -1. Example Walkthrough For number 695 and p = 2: 6^2 + ...

Read More

Reverse mapping an object in JavaScript

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

Reverse mapping an object in JavaScript involves swapping the keys and values of an object. This technique is useful when you need to look up keys based on their values. Suppose we have an object like this: const products = { "Pineapple": 38, "Apple": 110, "Pear": 109 }; All the keys are unique in themselves and all the values are unique in themselves. We need to write a function that accepts a value and returns its corresponding key. Method 1: Using Object.keys() with map() ...

Read More

Counting number of 9s encountered while counting up to n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 445 Views

We need to write a JavaScript function that counts how many times the digit "9" appears when counting from 0 to a given number n. For example, counting from 0 to 100 includes numbers like 9, 19, 29, 90, 91, 99, etc., where "9" appears multiple times. Problem We are required to write a JavaScript function that takes in a number n. Our function should count and return the number of times we will have to use the digit 9 while counting from 0 to n. Example Following is the code − const num ...

Read More

Add matching object values in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 444 Views

In JavaScript, you can add matching object values by iterating through an array of objects and accumulating values for common keys. This is useful for aggregating data from multiple objects. Consider an array of objects like this: const arr = [{a: 2, b: 5, c: 6}, {a: 3, b: 4, d: 1}, {a: 1, d: 2}]; console.log("Input array:", arr); Input array: [ { a: 2, b: 5, c: 6 }, { a: 3, b: 4, d: 1 }, { a: 1, d: 2 } ] Each object has unique properties within itself, ...

Read More

Checking if a string contains all unique characters using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 474 Views

Problem We are required to write a JavaScript function that takes in a string and returns true if all the characters in the string appear only once and false otherwise. Method 1: Using indexOf() and lastIndexOf() This approach compares the first and last occurrence of each character. If they differ, the character appears multiple times. const str = 'thisconaluqe'; const allUnique = (str = '') => { for(let i = 0; i < str.length; i++){ const el = str[i]; ...

Read More

Summing up unique array values in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 202 Views

We are required to write a JavaScript function that takes in an array of numbers that may contain some duplicate numbers. Our function should return the sum of all the unique elements (elements that only appear once in the array) present in the array. Problem Understanding If the input array is: const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, 11]; The unique elements (appearing only once) are: 3, 7, 4, 11. Their sum is 3 + 7 + 4 + 11 = 25. Using indexOf() and lastIndexOf() We ...

Read More

Is the reversed number a prime number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 353 Views

We are required to write a JavaScript function that takes in a number and returns true if the reverse of that number is a prime number, false otherwise. Problem Understanding To solve this problem, we need two helper functions: A function to reverse the digits of a number A function to check if a number is prime Solution Implementation Here's the complete solution with helper functions: const num = 13; const findReverse = (num) => { return +num .toString() .split('') .reverse() .join(''); }; const isPrime = (num) => { if (num

Read More
Showing 1971–1980 of 8,010 articles
« Prev 1 196 197 198 199 200 801 Next »
Advertisements