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
Web Development Articles
Page 256 of 801
Finding the greatest and smallest number in a space separated string of numbers using JavaScript
We are required to write a JavaScript function that takes in a string containing numbers separated by spaces and returns a string with only the greatest and smallest numbers separated by a space. Problem Statement Given a space-separated string of numbers, find the maximum and minimum values and return them as a formatted string. Input: '5 57 23 23 7 2 78 6' Expected Output: '78 2' Because 78 is the greatest and 2 is the smallest number in the string. Using Array.reduce() Method The most efficient approach uses ...
Read MorePreparing numbers from jumbled number names in JavaScript
Problem Suppose we have the following jumbled number name string: const str = 'TOWNE'; If we rearrange this string, we can find two number names in it: 2 (TWO) and 1 (ONE). Therefore, we expect an output of 21. We need to write a JavaScript function that takes in one such string and returns the numbers present in the string arranged in ascending order. Approach The solution involves: Creating a mapping of number words to their numeric values Generating permutations to check if number words can ...
Read MoreDisplaying likes on a post wherein array specifies the names of people that liked a particular post using JavaScript
We need to write a JavaScript function that takes an array of names representing people who liked a post. The function should format the output based on the number of likes: show all names for 3 or fewer likes, or show the first two names plus the remaining count for more than 3 likes. Problem Statement Create a function that displays likes in a user-friendly format: 0 likes: "no one likes this" 1 like: "John likes this" 2 likes: "John and Mary like this" 3 likes: "John, Mary and Bob like this" 4+ likes: "John, Mary and ...
Read MoreFinding nth element of an increasing sequence using JavaScript
Consider an increasing sequence which is defined as follows: The number seq(0) = 1 is the first one in seq. For each x in seq, then y = 2 * x + 1 and z = 3 * x + 1 must be in seq too. There are no other numbers in seq. Therefore, the first few terms of this sequence will be: [1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...] We are required to write a function ...
Read MoreFinding sum of remaining numbers to reach target average using JavaScript
Problem We are required to write a JavaScript function that takes in an array of numbers and a single number. Our function should find that very number which should be pushed to the array so that its average equals the number specified by the second argument. Understanding the Formula To find the number that makes the average equal to the target, we use this mathematical approach: Current sum of array elements New array length will be (current length + 1) Required total sum = target × new array length ...
Read MoreIsosceles triangles with nearest perimeter using JavaScript
Almost Isosceles Triangle An Almost Isosceles Integer Triangle is a triangle where all side lengths are integers and two sides are almost equal, with their absolute difference being exactly 1 unit of length. Problem Statement We need to write a JavaScript function that takes a number representing the desired perimeter of a triangle. The function should find an almost isosceles triangle whose perimeter is nearest to the input perimeter. For example, if the desired perimeter is 500, the almost isosceles triangle with the nearest perimeter will be [167, 166, 167] with perimeter 500. Understanding Almost ...
Read MoreMoving all zeroes present in the array to the end in JavaScript
We are required to write a JavaScript function that takes in an array of literals that might contain some 0s. Our function should tweak the array such that all the zeroes are pushed to the end and all non-zero elements hold their relative positions. Problem Given an array containing zeros and non-zero elements, we need to move all zeros to the end while maintaining the relative order of non-zero elements. Method 1: Using Two-Pass Approach This approach first collects non-zero elements, then adds zeros at the end: const arr = [5, 0, 1, 0, ...
Read MoreAll ways of balancing n parenthesis in JavaScript
Problem We are required to write a JavaScript function that takes in a number n. Our function should return an array showing all the ways of balancing n parenthesis. For example, for n = 3, the output will be: ["()()()", "(())()", "()(())", "(()())", "((()))"] Approach We use a recursive backtracking approach where we keep track of how many opening and closing parentheses we can still use. At each step, we can add an opening parenthesis if we have any left, or a closing parenthesis if it would create a valid balance. Example ...
Read MoreFinding the missing number between two arrays of literals in JavaScript
Finding the missing number between two arrays is a common programming problem where one array is a shuffled version of another with exactly one element removed. Problem Statement We need to write a JavaScript function that takes two arrays: arr1 (original) and arr2 (shuffled duplicate with one missing element). The function should identify and return the missing element. Example const arr1 = [6, 1, 3, 6, 8, 2]; const arr2 = [3, 6, 6, 1, 2]; const findMissing = (arr1 = [], arr2 = []) => { const obj = ...
Read MoreLargest index difference with an increasing value in JavaScript
We need to write a JavaScript function that finds the largest difference between two indices j - i where arr[i] ≤ arr[j]. This means we're looking for the maximum distance between two positions where the value at the later position is greater than or equal to the value at the earlier position. Problem Statement Given an array of numbers, find the maximum value of (j - i) such that arr[i] ≤ arr[j] and i < j. Example Let's implement a solution that checks all possible pairs: const arr = [1, 2, 3, 4]; ...
Read More