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 214 of 801
Checking for special numbers in JavaScript
In JavaScript, checking for special numbers often involves mathematical operations on digits. A palindrome digit sum check determines if the sum of a number's digits forms a palindrome. Problem We need to write a JavaScript function that takes a number and returns true if the sum of its digits is a palindrome number, false otherwise. For example, with input 781296: const num = 781296; The expected output is: true Output Explanation The digit sum of 781296 is 7+8+1+2+9+6 = 33, which reads the same forwards and backwards (palindrome). ...
Read MoreSum JavaScript arrays repeated value
Suppose, we have an array of objects like this − const arr = [ {'TR-01':1}, {'TR-02':3}, {'TR-01':3}, {'TR-02':5}]; We are required to write a JavaScript function that takes in one such array and sums the value of all identical keys together. Therefore, the summed array should look like − const output = [ {'TR-01':4}, {'TR-02':8}]; Method 1: Using In-Place Modification This approach modifies the original array by tracking duplicate keys and summing their values: const arr = [ {'TR-01':1}, {'TR-02':3}, {'TR-01':3}, {'TR-02':5}]; const sumDuplicate = arr => ...
Read MoreMerging nested arrays to form 1-d array in JavaScript
We are required to write a JavaScript function that takes in two nested arrays, arr1 and arr2, as the first and the second argument. Our function should create and return a third array that contains all the elements of arr1 and arr2 but flattened to single dimension For example, if the input to the function is — const arr1 = [ 1, [ 2, [ 4, 5, [ ...
Read MoreCompare arrays using Array.prototype.every() in JavaScript
We are required to write a JavaScript function that takes in two arrays of literals. Then our function should return true if all the elements of first array are included in the second array, irrespective of their count, false otherwise. We have to use Array.prototype.every() method to make these comparisons. Syntax array.every(callback(element, index, array), thisArg) How Array.every() Works The every() { const areEqual = arr1.every(el => { return arr2.includes(el); }); return areEqual; }; ...
Read MoreRemoving comments from array of string in JavaScript
We are required to write a JavaScript function that takes in array of strings, arr, as the first argument and an array of special characters, starters, as the second argument. The starter array contains characters that can start a comment. Our function should iterate through the array arr and remove all the comments contained in the strings. Problem Example For example, if the input to the function is: const arr = [ 'red, green !blue', 'jasmine, #pink, cyan' ]; const starters = ['!', '#']; Then the output ...
Read MoreComparing array elements keeping count in mind in JavaScript
Suppose, we have two arrays of literals that contain the same number of elements. We are supposed to write a function that checks whether or not the both arrays contain the same elements appearing for the same number of times. If the arrays fulfil this condition, we return true, false otherwise. We will create a copy of the second array, and start iterating over the first array. As we iterate, we will keep deleting the elements from the second array that are present in first array. If during iteration we encounter any element that isn't present in second ...
Read MoreBreaking camelCase syntax in JavaScript
We need to write a JavaScript function that takes a camelCase string and converts it into a readable format by adding spaces before uppercase letters. Our function should construct and return a new string that splits the input string using a space between words. Problem Example For example, if the input to the function is: Input const str = 'thisIsACamelCasedString'; Expected Output 'this Is A Camel Cased String' Solution Using String Iteration The approach iterates through each character and adds a space before uppercase letters (except the first character): ...
Read MoreSort nested array containing objects ascending and descending according to date in JavaScript
Suppose we have a JSON Object that contains a nested array like this: const arr = { "DATA": [ { "BookingID": "9513", "DutyStart": "2016-02-11 12:00:00" }, { "BookingID": "91157307", "DutyStart": "2016-02-11 13:00:00" }, { "BookingID": "95117317", "DutyStart": "2016-02-11 13:30:00" ...
Read MoreSearch a complex object by id property in JavaScript
Suppose we have a complex JSON Object like this − const obj = { "id": "0001", "fieldName": "sample1", "fieldValue": "0001", "subList": [ { "id": 1001, "fieldName": "Sample Child 1", "fieldValue": "1001", "subList": [] }, { "id": 1002, "fieldName": "Sample Child 2", ...
Read MoreSorting numbers based on their digit sums in JavaScript
We are required to write a JavaScript function that takes in an array of positive integers and sorts them based on their digit sums in descending order. Numbers with higher digit sums appear first. Problem Statement Given an array of positive integers, sort the array so that numbers with the highest digit sum come first, followed by numbers with lesser digit sums. For example, if the input array is: const arr = [5, 34, 1, 13, 76, 8, 78, 101, 57, 565]; The output should be: [565, 78, 76, 57, 8, ...
Read More