Web Development Articles

Page 168 of 801

Pairing an array from extreme ends in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 237 Views

We are required to write a JavaScript function that takes in an array of Number / String literals and returns another array of arrays. With each subarray containing exactly two elements, the nth element from start and nth from last. For example: If the array is − const arr = [1, 2, 3, 4, 5, 6]; Then the output should be − [[1, 6], [2, 5], [3, 4]] Example The code for this will be − const arr = [1, 2, 3, 4, 5, 6]; const edgePairs ...

Read More

Generating random string of specified length in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 528 Views

We are required to write a JavaScript function that takes in a number n and returns a random string of length n containing no other than the 26 English lowercase alphabets. Therefore, let's write the code for this function − Example The code for this will be − const num = 8; const randomNameGenerator = num => { let res = ''; for(let i = 0; i < num; i++){ const random = Math.floor(Math.random() * 26); ...

Read More

Finding nearest prime to a specified number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 308 Views

We are required to write a JavaScript function that takes in a number and returns the first prime number that appears after n. For example: If the number is 24, then the output should be 29. Therefore, let's write the code for this function − Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples include 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, etc. Example The code for this will be − const num = ...

Read More

Picking the odd one out in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 610 Views

We are required to write a JavaScript function that takes in an array of literals that contains all similar elements but one. Our function should return the unlike number. Therefore, let's write the code for this function − Example The code for this will be − const arr = [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]; // considering that the length of array is at least 3 const findUnlike = arr => { for(let i = 1; i < arr.length-1; i++){ ...

Read More

Deleting the last vowel from a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 293 Views

We are required to write a JavaScript function that takes in a string and returns a new string with the last vowel of each word removed. For example: If the string is − const str = 'This is an example string'; Then the output should be − const output = 'Ths s n exampl strng'; Therefore, let's write the code for this function − Example The code for this will be − const str = 'This is an example string'; const removeLast = word => { ...

Read More

Sorting the numbers from within in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 198 Views

We are required to write a JavaScript function that takes in an array of numbers and reorders the digits of all the numbers internally in a specific order (let's say in ascending order for the sake of this problem). For example: If the array is − const arr = [543, 65, 343, 75, 567, 878, 87]; Then the output should be − const output = [345, 56, 334, 57, 567, 788, 78]; Therefore, let's write the code for this function − Example The code for this will be − ...

Read More

Constructing array from string unique characters in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 191 Views

We are required to write a JavaScript function that takes in a string and starts mapping its characters from 0. And every time, the function encounters a unique (non-duplicate) character it should increase the mapping count by 1 otherwise it should map the same number for duplicate characters. For example: If the string is − const str = 'heeeyyyy'; Then the output should be − const output = [0, 1, 1, 1, 2, 2, 2, 2]; Therefore, let's write the code for this function − Example The code ...

Read More

Finding pandigital numbers using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 249 Views

A pandigital number is a number that contains all digits (0-9) at least once. In this tutorial, we'll create a JavaScript function to check if a given number string is pandigital. What is a Pandigital Number? A pandigital number must contain every digit from 0 to 9 at least once. For example, "1234567890" is pandigital, while "123456789" is not (missing 0). Example Let's implement a function to check if a number string is pandigital: const numStr1 = '47458892414'; const numStr2 = '53657687691428890'; const isPandigital = numStr => { let ...

Read More

Finding sum of all unique elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 258 Views

In JavaScript, you can sum duplicate elements by counting their occurrences and multiplying each unique value by its count. This technique is useful for consolidating arrays with repeated values. For example, if the input array is: const input = [1, 3, 1, 3, 5, 7, 5, 4]; The output should be: [2, 6, 10, 7, 4] This means: 1 appears 2 times (1×2=2), 3 appears 2 times (3×2=6), 5 appears 2 times (5×2=10), while 7 and 4 appear once each. Using Map to Count and Sum The most efficient ...

Read More

Deviations in two JavaScript arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 237 Views

We have two arrays of numbers and need to find elements that exist in one array but not in both. This is called finding the symmetric difference or deviation between arrays. const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; We need to write a JavaScript function that takes two arrays and returns elements that are not common to both arrays. Using indexOf() Method The basic approach uses indexOf() to check if elements exist in the other array: const arr1 ...

Read More
Showing 1671–1680 of 8,010 articles
« Prev 1 166 167 168 169 170 801 Next »
Advertisements