Web Development Articles

Page 214 of 801

Checking for special numbers in JavaScript

Ranjit Kumar
Ranjit Kumar
Updated on 15-Mar-2026 566 Views

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 More

Sum JavaScript arrays repeated value

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 352 Views

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 More

Merging nested arrays to form 1-d array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 384 Views

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 More

Compare arrays using Array.prototype.every() in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 231 Views

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 More

Removing comments from array of string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 420 Views

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 More

Comparing array elements keeping count in mind in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 209 Views

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 More

Breaking camelCase syntax in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 315 Views

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 More

Sort nested array containing objects ascending and descending according to date in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

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 More

Search a complex object by id property in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 485 Views

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 More

Sorting numbers based on their digit sums in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 626 Views

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
Showing 2131–2140 of 8,010 articles
« Prev 1 212 213 214 215 216 801 Next »
Advertisements