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 362 of 840
Convert array into array of subarrays - JavaScript
We are required to write a JavaScript function that takes in an array of literals and returns a new array that have elements of the original array chunked into subarrays of length exactly 2. Now if the length of original array is not exactly divisible by 2, then the last subarray should have only one element. For example, if the input array is: const arr = [1, 2, 3, 4, 5, 6, 7]; Then the output should be: const output = [[1, 2], [3, 4], [5, 6], [7]] Using Loop-based ...
Read MoreHow to recognize when to use : or = in JavaScript?
The colon (:) is used to define properties in objects, while the equal sign (=) is used to assign values to variables. Understanding when to use each is fundamental in JavaScript. Using Colon (:) in Objects The colon separates property names from their values when creating object literals: var studentDetails = { "studentId": 101, "studentName": "John", "studentSubjectName": "Javascript", "studentCountryName": "US" }; console.log(studentDetails); { studentId: 101, studentName: 'John', studentSubjectName: 'Javascript', ...
Read MoreFinding union of two sets in JavaScript
The union of two sets is the combination of all unique elements from both sets. In JavaScript, we can find the union of two arrays using several approaches. What is Union Set Union Set is the set made by combining the elements of two sets. Therefore, the union of sets A and B is the set of elements in A, or B, or both. For example − If we have two sets denoted by two arrays like this − const arr1 = [1, 2, 3]; const arr2 = [100, 2, 1, 10]; ...
Read MoreIs a number sum of two perfect squares in JavaScript
Perfect Square Numbers A natural number in mathematics is called a perfect square if it can be obtained by multiplying any other natural number by itself. For instance, 9, 16, 81, 289 are all perfect squares because: 9 = 3 × 3 16 = 4 × 4 81 = 9 × 9 289 = 17 × 17 We need to write a JavaScript function that takes a natural number as input and determines whether it can be expressed ...
Read MoreFinding the 1-based index of a character in alphabets using JavaScript
We are required to write a JavaScript function that takes in a lowercase English alphabet character. Our function should return the character's 1-based index in the alphabets. Problem Given a lowercase English alphabet character, we need to find its position in the alphabet where 'a' = 1, 'b' = 2, 'c' = 3, and so on. Method 1: Using String indexOf() We can create a string containing all alphabets and use indexOf() to find the position: const char = 'j'; const findCharIndex = (char = '') => { const legend ...
Read MoreHow to select the middle of an array? - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns the middlemost element(s) of the array. For arrays with odd length, there's one middle element. For arrays with even length, there are two middle elements. For example, if the array is: const arr = [1, 2, 3, 4, 5, 6, 7]; Then the output should be [4] (the middle element at index 3). Using Array Prototype Method We can extend the Array prototype to add a middle() method that returns the middle element(s): ...
Read MoreSort an array and place a particular element as default value in JavaScript
We are required to write a JavaScript function that takes in an array of literal values as the first argument and a string as the second argument. Our function should sort the array alphabetically but keeping the string provided as the second argument (if it exists in the array) as the first element, irrespective of the text it contains. Example The code for this will be − const arr = ["Apple", "Orange", "Grapes", "Pineapple", "None", "Dates"]; const sortKeepingConstants = (arr = [], text = '') => { const sorter = (a, ...
Read MoreConverting ASCII to hexadecimal in JavaScript
Converting ASCII characters to hexadecimal is a common task in JavaScript. Each character has an ASCII code that can be represented as a hexadecimal value. Understanding ASCII to Hex Conversion Each character in a string has an ASCII code. For example, '1' has ASCII code 49, '5' has 53, and '9' has 57. Converting to hexadecimal: 49 → 31, 53 → 35, 57 → 39. Example Here's how to convert ASCII characters to their hexadecimal representation: const str = '159'; const convertToHexa = (str = '') => { const res ...
Read MoreFinding longest line of 1s in a matrix in JavaScript
Suppose, we have a binary matrix (an array of arrays that contains only 0 or 1) like this: const arr = [ [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 1] ]; We need to write a JavaScript function that takes a binary matrix as input and finds the longest line of consecutive ones. The line can be horizontal, vertical, diagonal, or anti-diagonal. For the above array, the output should be 3 because the longest line starts from arr[0][1] and spans diagonally to ...
Read MoreHow to find the biggest number in an array around undefined elements? - JavaScript
We are required to write a JavaScript function that takes in an array that contains some numbers, some strings and some falsy values. Our function should return the biggest Number from the array. For example − If the input array is the following with some undefined values − const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; Then the output should be 65 Solution Approach We'll filter out non-numeric values and find the maximum among valid numbers. The key is to properly identify numeric values while handling ...
Read More