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
Articles by AmitDiwan
Page 320 of 840
Reversing 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 MoreKilling Enemy in JavaScript
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 MoreFinding the largest 5 digit number within the input number using JavaScript
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 MoreDifference between two strings JavaScript
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 MoreDecimal to binary conversion using recursion in JavaScript
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 MoreFind Equivalent Value and Frequency in Array in JavaScript
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 MoreApplying f(x) to each array element in JavaScript
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 MoreReturning array values that are not odd in JavaScript
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 MoreCombine two different arrays in JavaScript
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 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 More