Web Development Articles

Page 179 of 801

Reversing the order of words of a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 505 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 188 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 733 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 260 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 270 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 427 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 1K+ 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

Calculate the hypotenuse of a right triangle in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 796 Views

We are required to write a JavaScript function that takes in two numbers. The first number represents the length of the base of a right triangle and the second is perpendicular. The function should then compute the length of the hypotenuse based on these values. For example − If base = 8, perpendicular = 6 Then the output should be 10 The Pythagorean Theorem The hypotenuse is calculated using the Pythagorean theorem: c² = a² + b², where c is the hypotenuse and a, b are the other two sides. ...

Read More

JavaScript function that generates all possible combinations of a string

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 852 Views

We are required to write a JavaScript function that takes in a string as the only argument. The function should generate an array of strings that contains all possible combinations of characters from the string. Understanding the Problem The function uses a bitwise approach to generate all possible combinations. It first extracts individual characters, then uses binary representation to determine which characters to include in each combination. Example Following is the code − const str = 'Delhi'; const allCombinations = (str1 = '') => { const arr = []; ...

Read More
Showing 1781–1790 of 8,010 articles
« Prev 1 177 178 179 180 181 801 Next »
Advertisements