Javascript Articles

Page 80 of 534

Can form target array from source array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 231 Views

We are given an array of distinct integers, let's say arr, and another array of integer arrays, let say sourceArr. In the sourceArr array, the integers are distinct. We should write a function that forms arr by concatenating the arrays in sourceArr in any order. However, we cannot reorder the integers inside of any subarray in the sourceArr. We should return true if it is possible to form the array arr from sourceArr, false otherwise. For example: const arr = [23, 67, 789]; const sourceArr = [[23], [789, 67]]; The function should return ...

Read More

Sum of all multiples in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 332 Views

We need to write a JavaScript function that takes a number n as the first argument, followed by any number of divisor arguments. The function should sum all numbers up to n that are divisible by any of the specified divisors. Problem Statement Given a number n and multiple divisors, find the sum of all numbers from 1 to n that are multiples of any of the divisors. For example, if we call: sumMultiples(15, 2, 3) We need to find numbers up to 15 that are divisible by either 2 or 3: ...

Read More

Reversing the order of words of a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 480 Views

We are required to write a JavaScript function that takes in a string as the only argument. The function should reverse the order of the words in the string and return the new string. The only condition is that we cannot use the inbuilt array method reverse(). For example − If the input string is − const str = 'this is a string'; Then the output string should be − string a is this Using Manual Array Reversal We can split the string into words, manually reverse ...

Read More

Awkward behaviour of delete operator on arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 179 Views

The delete operator in JavaScript is actually an object operator (used with objects). But since arrays are also indexed objects in JavaScript, we can use the delete operator with arrays as well. However, this leads to some awkward behavior that can confuse developers. Consider the following array of literals: const arr = ['a', 'b', 'c', 'd', 'e']; Example Let us now execute the following program and observe the unexpected output: const arr = ['a', 'b', 'c', 'd', 'e']; delete arr[4]; console.log(arr); console.log(arr.length); Output [ 'a', 'b', 'c', ...

Read More

Recursive Staircase problem in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 726 Views

The staircase problem is a classic dynamic programming problem in computer science. Given n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 or 2 stairs at a time, and we need to count the number of ways to reach the top. We'll write a JavaScript function that takes a number n representing the number of stairs and returns the total number of ways to climb them. Understanding the Problem This problem follows the Fibonacci sequence pattern. For n stairs: 1 stair: 1 way (take 1 step) ...

Read More

Uncamelising a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 253 Views

We are required to write a JavaScript function that takes in a string as the first argument and a separator character as the second argument. The first string is guaranteed to be a camelCased string. The function should convert the case of the string by separating the words by the separator provided as the second argument. For example − If the input string is − const str = 'thisIsAString'; const separator = '_'; Then the output string should be − const output = 'this_is_a_string'; Using Custom Logic This ...

Read More

Reversing a string using for loop in JavaScript

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

We are required to write a JavaScript function that takes in a string as the only argument. The function should construct a new reversed string based on the input string using a for loop. Syntax for (let i = string.length - 1; i >= 0; i--) { reversedString += string[i]; } Example Following is the code − const str = 'this is the original string'; const reverseString = (str = '') => { let reverse = ''; const { length: ...

Read More

Difference between sum and product of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 258 Views

We are required to write a JavaScript function that takes in an array of Numbers as the only argument. The function should calculate the sum of all numbers in the array and the product of all numbers. Then the function should return the absolute difference between the sum and the product. Example Following is the code − const arr = [1, 4, 1, 2, 1, 6, 3]; const sumProductDifference = (arr = []) => { const creds = arr.reduce((acc, val) => { let { ...

Read More

Flattening a deeply nested array of literals in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 417 Views

We are required to write a JavaScript function that takes in a nested array of literals as the only argument. The function should construct a new array that contains all the literal elements present in the input array but without nesting. For example − If the input array is − const arr = [ 1, 3, [5, 6, [7, [6, 5], 4], 3], [4] ]; Then the output array should be − [1, 3, 5, 6, 7, 6, 5, 4, 3, 4] Method 1: Recursive Approach ...

Read More

Converting ASCII to hexadecimal in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 983 Views

Converting ASCII characters to hexadecimal is a common task in JavaScript. Each character has an ASCII code that can be represented as a hexadecimal value. Understanding ASCII to Hex Conversion Each character in a string has an ASCII code. For example, '1' has ASCII code 49, '5' has 53, and '9' has 57. Converting to hexadecimal: 49 → 31, 53 → 35, 57 → 39. Example Here's how to convert ASCII characters to their hexadecimal representation: const str = '159'; const convertToHexa = (str = '') => { const res ...

Read More
Showing 791–800 of 5,340 articles
« Prev 1 78 79 80 81 82 534 Next »
Advertisements