AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 308 of 840

Changing the case of a string using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 255 Views

We are required to write a JavaScript function that takes in a string and converts it to snake case. Snake case is basically a style of writing strings by replacing the spaces with '_' and converting the first letter of each word to lowercase. Example The code for this will be − const str = 'This is a simple sentence'; const toSnakeCase = (str = '') => { const strArr = str.split(' '); const snakeArr = strArr.reduce((acc, val) => { ...

Read More

Matching odd even indices with values in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 274 Views

We are required to write a JavaScript function that takes in an array of numbers. The array given as an input to the function have two special properties − The length of the array will always be an even number. The number of even numbers and the number of odd numbers in the array will always be equal (i.e., both being equal to the half of the length of array) The function should shuffle the elements of the array such that all the even values occupy even indices ...

Read More

Vowel gaps array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 179 Views

We are required to write a JavaScript function that takes in a string with at least one vowel, and for each character in the string we have to map a number representing its nearest distance from a vowel. For example: If the string is − const str = 'vatghvf'; Output Then the output should be − const output = [1, 0, 1, 2, 3, 4, 5]; The logic is: 'v' is 1 position away from vowel 'a', 'a' is 0 (it's a vowel), 't' is 1 away from 'a', 'g' ...

Read More

Removing smallest subarray to make array sum divisible in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 165 Views

We are required to write a JavaScript function that takes in an array of positive integers as the first argument and a positive integer as the second argument. The function should figure out and return the length of the smallest subarray that we should delete from the original array in order to make its sum divisible by the number specified by the second argument. For example, if we have an array [3, 8, 2, 6] and want the sum divisible by 9, we need to remove the subarray [8, 2] of length 2. Problem Analysis The ...

Read More

Column sum of elements of 2-D arrays in JavaScript

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

We have an array of arrays and are required to write a function that takes in this array and returns a new array that represents the sum of corresponding elements of original array. If the original array is: [ [43, 2, 21], [1, 2, 4, 54], [5, 84, 2], [11, 5, 3, 1] ] Then the output should be: [60, 93, 30, 55] This means we add all elements at index 0 (43+1+5+11=60), all elements at index 1 (2+2+84+5=93), ...

Read More

Swapping letter with succeeding alphabet in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 255 Views

We are required to write a JavaScript function that takes in a string and changes every letter of the string from the English alphabets to its succeeding element. For example: If the string is − const str = 'how are you'; Output Then the output should be − const output = 'ipx bsf zpv' Therefore, let's write the code for this function − How It Works The algorithm converts each alphabetic character to its next letter in the alphabet. For 'z' and 'Z', it wraps around to 'a' ...

Read More

Merge two sorted arrays to form a resultant sorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 281 Views

We are required to write a JavaScript function that takes in two sorted arrays of numbers. The function should merge the two arrays together to form a resultant sorted array and return that array. For example, if the two arrays are: const arr1 = [2, 6, 6, 8, 9]; const arr2 = [1, 4, 5, 7]; Then the output array should be: [1, 2, 4, 5, 6, 6, 7, 8, 9] Method 1: Using Two Pointers Technique The most efficient approach is the two-pointers technique, which compares elements from both ...

Read More

Finding all solutions of a Diophantine equation using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 328 Views

Problem We need to write a JavaScript function that takes a number n and finds all integer solutions (x, y) for the Diophantine equation: x^2 - 4y^2 = n The function should return an array of all such pairs [x, y]. Mathematical Approach To solve x² - 4y² = n, we can factorize it as: x^2 - 4y^2 = (x + 2y)(x - 2y) = n Let a = x - 2y and b = x + 2y, then: a × b = n x = (a + ...

Read More

The n times dribbling strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 146 Views

We are required to write a JavaScript function that takes in a string and a number, say n, and the function should return a new string in which all the letters of the original string are repeated n times. For example: If the string is − const str = 'how are you' And the number n is 2. Output Then the output should be − const output = 'hhooww aarree yyoouu' Therefore, let's write the code for this function − Using String.prototype.repeat() The most straightforward approach is to iterate through each ...

Read More

How to merge two arrays with objects in one in JavaScript?

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

Suppose, we have two arrays of objects like these − const arr1 = [ {name:'test', lastname: 'test', gender:'f'}, {name:'test1', lastname: 'test1', gender:'f'}, {name:'test2', lastname: 'test2', gender:'m'} ]; const arr2 = [ {name:'test21', lastname: 'test21', gender:'f'}, {name:'test1', lastname: 'test1', gender:'f'}, {name:'test2', lastname: 'test2', gender:'m'}, {name:'test22', lastname: 'test22', gender:'m'} ]; console.log("Array 1:", arr1); console.log("Array 2:", arr2); Array 1: [ { name: 'test', lastname: 'test', gender: 'f' }, { name: 'test1', lastname: 'test1', gender: 'f' }, { ...

Read More
Showing 3071–3080 of 8,392 articles
« Prev 1 306 307 308 309 310 840 Next »
Advertisements