Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Javascript Articles
Page 80 of 534
Can form target array from source array JavaScript
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 MoreSum of all multiples in JavaScript
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 MoreReversing the order of words of a string in JavaScript
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 MoreAwkward behaviour of delete operator on arrays in JavaScript
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 MoreRecursive Staircase problem in JavaScript
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 MoreUncamelising a string in JavaScript
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 MoreReversing a string using for loop in JavaScript
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 MoreDifference between sum and product of an array in JavaScript
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 MoreFlattening a deeply nested array of literals in JavaScript
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 MoreConverting ASCII to hexadecimal in JavaScript
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