AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 320 of 840

Reversing the order of words of a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 504 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

Killing Enemy in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 345 Views

In JavaScript, we can solve the "killing enemy" problem by finding the optimal position to place a bomb that kills the maximum number of enemies in a 2D grid. The bomb destroys all enemies in the same row and column until it hits a wall. Problem Statement Given a 2D grid where each cell is either a wall 'W', an enemy 'E', or empty '0', we need to find the maximum enemies we can kill using only one bomb. The bomb can only be placed in empty cells and kills all enemies in the same row and column ...

Read More

Finding the largest 5 digit number within the input number using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 506 Views

We need to write a JavaScript function that takes in a string number of at least five digits and returns the greatest sequence of five consecutive digits found within the input number. Problem Statement Given a number string, find all possible 5-digit consecutive sequences and return the largest one as a number. Example Let's implement the solution step by step: const num = '123546544'; const findGreatestFiveDigit = (num = '') => { const str = num.toString(); const arr = []; ...

Read More

Difference between two strings JavaScript

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

We are given two strings, say s and t. String t is generated by random shuffling string s and then add one more letter at a random position. We are required to write a JavaScript function that takes both these strings and returns the letter that was added to t. For example − If the input strings are − const s = "abcd", t = "abcde"; Then the output should be − const output = "e"; because 'e' is the letter that was added. Method 1: Using XOR ...

Read More

Decimal to binary conversion using recursion in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 514 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should use recursion to construct a string representing the binary notation of that number. For example − f(4) = '100' f(1000) = '1111101000' f(8) = '1000' How Binary Conversion Works Binary conversion involves repeatedly dividing a number by 2 and collecting the remainders. The remainders, read in reverse order, form the binary representation. Decimal 4 to Binary 4 ÷ 2 ...

Read More

Find Equivalent Value and Frequency in Array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 143 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument. The function should check whether there exists an integer in the array such that its frequency is same as its value. If there exists at least one such integer, we should return that integer otherwise we should return -1. For example − If the input array is − const arr = [3, 4, 3, 8, 4, 9, 7, 4, 2, 4]; Then the output should be − 4 The ...

Read More

Applying f(x) to each array element in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 210 Views

When working with arrays in JavaScript, you often need to apply mathematical functions to each element and return a sorted result. This article demonstrates how to apply a quadratic function f(x) = ax² + bx + c to array elements. Problem Statement Given a mathematical function: f(x) = ax² + bx + c Where a, b, and c are constants, we need to create a JavaScript function that: Takes a sorted array of integers as the first argument Takes constants a, b, and c as the next three arguments Applies f(x) to each ...

Read More

Returning array values that are not odd in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 197 Views

Problem We are required to write a JavaScript function that takes in an array of numbers. Our function should construct and return a new array that contains all the numbers of the input array that are not odd (i.e., even numbers). Example Following is the code − const arr = [5, 32, 67, 23, 55, 44, 23, 12]; const findNonOdd = (arr = []) => { const res = []; for(let i = 0; i < arr.length; i++){ ...

Read More

Combine two different arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 469 Views

Suppose we have two arrays, the first array contains the scheduled date for some events and the second array contains the names of those events, like this − const dates = [ { id: "1", date: "2017-11-07" }, { id: "1", date: "2017-11-08" }, { ...

Read More

Awkward behaviour of delete operator on arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 186 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
Showing 3191–3200 of 8,392 articles
« Prev 1 318 319 320 321 322 840 Next »
Advertisements