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 470 of 840
JavaScript - convert array with null value to string
In this article, we will learn to convert an array with a null value to a string in Javascript. Handling arrays containing null, undefined, and falsy values in JavaScript is a frequent challenge. The default methods might not behave as expected when converting such arrays to strings, requiring custom solutions for meaningful string representations. Problem Statement Given an array containing null, undefined, empty strings, and other values, the task is to concatenate its elements into a single string while excluding the unwanted values. Following is our array, with some null and undefined values − Input ...
Read MoreFinding the intersection of arrays of strings - JavaScript
We need to find the intersection of two arrays of strings and return an array containing the common elements. Each element in the result should appear as many times as it shows in both arrays. For example − If input is − arr1 = ['hello', 'world', 'how', 'are', 'you']; arr2 = ['hey', 'world', 'can', 'you', 'rotate']; Then the output should be − ['world', 'you'] Approach For unsorted arrays, we need to check every value of the first array against the second array. This approach has O(n²) time complexity. We ...
Read MoreFormatting text to add new lines in JavaScript and form like a table?
To format text with new lines in JavaScript and create table-like output, use the map() method combined with join(''). The '' character creates line breaks in console output. Syntax array.map(element => `formatted string`).join('') Example: Creating a Table-like Format let studentDetails = [ [101, 'John', 'JavaScript'], [102, 'Bob', 'MySQL'], [103, 'Alice', 'Python'] ]; // Create header let tableHeader = '||Id||Name||Subject||'; // Format data rows let tableRows = studentDetails.map(student => `|${student.join('|')}|` ).join(''); // Combine header ...
Read MoreDividing an array – JavaScript
Let's say, we are required to write a function that takes in an array arr of string / number literals as the first argument and a number n as second argument. We are required to return an array of n subarrays, each of which contains at most arr.length / n elements. And the distribution of elements should be like this − The first element goes in the first subarray, second in second, third in third and so on. Once we have one element in each subarray, we again start ...
Read MoreTextDecoder and TextEncoder in Javascript?
TextEncoder and TextDecoder are modern JavaScript APIs that handle text encoding and decoding operations. TextEncoder converts strings to UTF-8 bytes, while TextDecoder converts byte arrays back to strings with support for multiple character encodings. TextEncoder Overview TextEncoder converts JavaScript strings into UTF-8 encoded Uint8Array. It only supports UTF-8 encoding and provides a simple encode() method. TextEncoder Example ...
Read MoreCounting how many times an item appears in a multidimensional array in JavaScript
We have a nested array of strings and we have to write a function that accepts the array and a search string and returns the count of the number of times that string appears in the nested array. Therefore, let's write the code for this, we will use recursion here to search inside of the nested array and the code for this will be − Example const arr = [ "apple", ["banana", "strawberry", "dsffsd", "apple"], "banana", ["sdfdsf", "apple", ["apple", ["nonapple", "apple", ["apple"]]]] ...
Read MoreCheck whether a number is a Fibonacci number or not JavaScript
We are required to write a JavaScript function that takes in a number and returns a boolean based on the fact whether or not it comes in the fibonacci series. For example − If the function call is like this − fibonacci(12); fibonacci(89); fibonacci(55); fibonacci(534); Then the output should be − false true true false What is the Fibonacci Series? The Fibonacci series is a sequence where each number is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Read MoreDelete duplicate elements based on first letter – JavaScript
We need to write a JavaScript function that removes duplicate strings based on their first letter, keeping only the first occurrence of each starting letter. For example, if we have an array like: const arr = ['Apple', 'Jack', 'Army', 'Car', 'Jason']; We should keep only one string for each starting letter. Since 'Apple' and 'Army' both start with 'A', we keep only 'Apple' (first occurrence). Similarly, 'Jack' and 'Jason' both start with 'J', so we keep only 'Jack'. Problem with Direct Array Modification The original approach has a bug - modifying an array ...
Read MoreCalculating average of an array in JavaScript
Calculating the average of an array is a common task in JavaScript. The average is computed by summing all elements and dividing by the array length. Basic Formula Average = (Sum of all elements) / (Number of elements) Method 1: Using forEach Loop Calculate Array Average Calculating Average of an Array Array: Calculate Average ...
Read MoreGet minimum number without a Math function JavaScript
We need to find the smallest number from a set of numbers without using JavaScript's built-in Math.min() function. This requires implementing our own comparison logic. Approach Using While Loop We'll iterate through all numbers and keep track of the smallest value found so far, updating it whenever we encounter a smaller number. Example const numbers = [12, 5, 7, 43, -32, -323, 5, 6, 7, 767, 23, 7]; const findMin = (...numbers) => { let min = Infinity, len = 0; while(len < numbers.length) { ...
Read More