Javascript Articles

Page 83 of 534

Converting whitespace string to url in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 703 Views

In web URLs, browsers automatically replace spaces with '%20' for proper encoding. JavaScript provides multiple methods to convert whitespace characters to URL-encoded format. We need to write a function that takes a string and replaces all whitespace characters with '%20'. For example, if the input string is: const str = 'some extra Space'; Then the output should be: const output = 'some%20extra%20%20Space'; Using Manual Loop Method This approach iterates through each character and manually replaces spaces: const str = 'some extra Space'; const replaceWhitespace = (str = ...

Read More

Finding elements whose successors and predecessors are in array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 514 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument. The function should construct and return a new array that contains all such elements from the original array whose successor and predecessor are both present in the array. This means, if any element num is in the original array, it should be included in the result array if and only if num - 1 and num + 1 are also present in the array. For example, if the input array is: const arr = ...

Read More

Finding confusing number within an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 300 Views

A number in an array is confusing if it becomes another number which is also present in the array after we rotate it by 180 degrees. When rotated, digits transform as follows: 0→0, 1→1, 6→9, 8→8, 9→6. Other digits (2, 3, 4, 5, 7) cannot be rotated to form valid numbers. Understanding Confusing Numbers For a number to be confusing: It must contain only the digits 0, 1, 6, 8, 9 When rotated 180 degrees, it must form a valid number within our range The rotated number must also be present in the array Example ...

Read More

Checking digit sum of smallest number in the array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 266 Views

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument. The function should first pick the smallest number from the array and then calculate the sum of all the digits of the number. If the digit sum of that number is even, we should return true, false otherwise. Problem Example If the input array is: const arr = [12, 657, 23, 56, 34, 678, 42]; Then the output should be: const output = false; Because the smallest ...

Read More

Finding the maximum number using at most one swap in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 280 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The task of our function is to perform at most one swap between any two digits of the number and yield the maximum possible number. If the number is already the maximum possible number, we should return the number itself. For example, if the input number is: const num = 1625; Then the output should be: const output = 6125; We swapped 1 and 6, and this is the only ...

Read More

Finding longest line of 1s in a matrix in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 262 Views

Suppose, we have a binary matrix (an array of arrays that contains only 0 or 1) like this: const arr = [ [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 1] ]; We need to write a JavaScript function that takes a binary matrix as input and finds the longest line of consecutive ones. The line can be horizontal, vertical, diagonal, or anti-diagonal. For the above array, the output should be 3 because the longest line starts from arr[0][1] and spans diagonally to ...

Read More

Finding the sum of two numbers without using '+', '-', '/', '*' in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 987 Views

We are required to write a JavaScript function add() that takes in two numbers m and n. The function should, without using the four basic arithmetic operations add the two numbers taken as input and return the sum. The Challenge Without using +, -, *, or /, we need to implement addition using bitwise operations. This approach mimics how computers perform addition at the hardware level using binary logic. Example The code for this will be − const m = 67, n = 33; const add = (x, y) => { while(y !== 0){ let carry = x & y; x = x ^ y; y = carry

Read More

Finding the longest substring uncommon in array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 245 Views

In JavaScript, finding the longest uncommon subsequence in an array involves identifying the longest string that appears only once or doesn't exist as a subsequence in other strings. Understanding Subsequence A subsequence is a sequence derived from another sequence by deleting some characters without changing the order of remaining elements. Any string is a subsequence of itself, and an empty string is a subsequence of any string. Problem Definition We need to find the length of the longest uncommon subsequence among an array of strings. An uncommon subsequence is a subsequence of one string that is ...

Read More

Deriving Random10() function from Random7() in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 321 Views

Suppose we have a random7() function that generates random numbers from 1 to 7. We need to create a random10() function that generates random numbers from 1 to 10, using only the random7() function. Problem const random7 = () => Math.ceil(Math.random() * 7); This function yields a random number between 1 and 7 (inclusive) every time we call it. We need to write a random10() function that returns random numbers between 1 and 10 (inclusive) using only this random7() function. Using Rejection Sampling (Optimal Solution) The most efficient approach uses rejection sampling. We ...

Read More

Weight sum of a nested array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 539 Views

We are required to write a JavaScript function that takes in a nested array, arr (nested up to any level) as the only argument. The function should calculate the weighted sum of the nested array and return that sum. For calculating weighted sum, we multiply each element with its level of nesting and add throughout the array. Elements at the first level are multiplied by 1, second level by 2, and so on. Problem Example For example, if the input to the function is: const arr = [4, 7, [6, 1, [5, 2]]]; ...

Read More
Showing 821–830 of 5,340 articles
« Prev 1 81 82 83 84 85 534 Next »
Advertisements