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 414 of 801
Convert number to alphabet letter JavaScript
We are required to write a function that takes in a number between 1 and 26 (both inclusive) and returns the corresponding English alphabet for it. (capital case) If the number is out of this range return -1. For example: toAlpha(3) = C toAlpha(18) = R The ASCII Codes ASCII codes are the standard numerical representation of all the characters and numbers present on our keyboard and many more. The capital English alphabets are also mapped in the ASCII char codes, they start from 65 and go all the way up to 90, ...
Read MoreCompletely removing duplicate items from an array in JavaScript
We are required to write a function that takes in an array and returns a new array that have all duplicate values removed from it. The values that appeared more than once in the original array should not even appear for once in the new array. For example, if the input is: const arr = [23, 545, 43, 232, 32, 43, 23, 43]; The output should be: [545, 232, 32] Understanding indexOf() vs lastIndexOf() Array.prototype.indexOf() → Returns the index of first occurrence of searched ...
Read MoreFind duplicate element in a progression of first n terms JavaScript
When you have an array of first n natural numbers with one duplicate element, finding the duplicate efficiently is a common programming challenge. We'll explore two mathematical approaches that solve this in linear time. Method 1: Using Array.prototype.reduce() This approach uses a mathematical trick with the reduce function to find the duplicate in a single pass: const arr = [2, 3, 1, 2]; const duplicate = a => a.reduce((acc, val, ind) => val + acc - (ind + 1)) + a.length - 1; console.log(duplicate(arr)); 2 How the Reduce Method Works ...
Read MoreCompute the sum of elements of an array which can be null or undefined JavaScript
When working with arrays containing numbers, null, and undefined values, we need to compute sums while treating these falsy values as zero. This is a common scenario when processing data from APIs or user inputs. Let's say we have an array of arrays, each containing some numbers along with some undefined and null values. We need to create a new array that contains the sum of each corresponding sub-array elements, treating undefined and null as 0. Following is the sample array: const arr = [[ 12, 56, undefined, 5 ], [ ...
Read MoreHow to get the product of two integers without using * JavaScript
We are required to write a function that takes in two numbers and returns their product, but without using the (*) operator. Method 1: Using Division Operator We know that multiplication and division are inverse operations, so if we divide a number by another number's inverse, it's the same as multiplying the two numbers. The mathematical concept: a × b = a ÷ (1 ÷ b) const a = 20, b = 45; const product = (a, b) => a / (1 / b); console.log(product(a, b)); 900 Method 2: Using ...
Read MoreReplace a letter with its alphabet position JavaScript
We are required to write a function that takes in a string, trims it off any whitespaces, converts it to lowercase and returns an array of numbers describing corresponding characters positions in the english alphabets, any whitespace or special character within the string should be ignored. Problem Example Input → 'Hello world!' Output → [8, 5, 12, 12, 15, 23, 15, 18, 12, 4] How It Works The algorithm processes each character by: Converting to lowercase to handle both cases uniformly Getting ASCII code using charCodeAt() ...
Read MoreHow to add a character to the beginning of every word in a string in JavaScript?
We need to write a function that takes two strings and returns a new string with the second argument prepended to every word of the first string. For example: Input → 'hello stranger, how are you', '@@' Output → '@@hello @@stranger, @@how @@are @@you' If the second argument is not provided, we'll use '#' as the default character. Solution Using split() and map() The most straightforward approach is to split the string into words, prepend the character to each word, and join them back: const str = 'hello stranger, how are ...
Read MoreHow to get only the first n% of an array in JavaScript?
We are required to write a function that takes in an array arr and a number n between 0 and 100 (both inclusive) and returns the n% part of the array. Like if the second argument is 0, we should expect an empty array, complete array if it's 100, half if 50, like that. And if the second argument is not provided it should default to 50. Therefore, the code for this will be − Syntax const byPercent = (arr, n = 50) => { const requiredLength = Math.floor((arr.length * n) / ...
Read MoreFind average of each array within an array JavaScript
We are required to write a function getAverage() that accepts an array of arrays of numbers and we are required to return a new array of numbers that contains the average of corresponding subarrays. Let's write the code for this. We will map over the original array, reducing the subarray to their averages like this − Example const arr = [[1, 54, 65, 432, 7, 43, 43, 54], [2, 3], [4, 5, 6, 7]]; const secondArr = [[545, 65, 5, 7], [0, 0, 0, 0], []]; const getAverage = (arr) => { ...
Read MoreRecursion in array to find odd numbers and push to new variable JavaScript
We are required to write a recursive function, say pushRecursively(), which takes in an array of numbers and returns an object containing odd and even properties where odd is an array of odd numbers from input array and even an array of even numbers from input array. This has to be done using recursion and no type of loop method should be used. Syntax const pushRecursively = (arr, len = 0, odd = [], even = []) => { // Base case: if we've processed all elements if (len ...
Read More