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 180 of 801
Return Vowels in a string in JavaScript
We are required to write a JavaScript function that takes in a string that might contain some alphabets. The function should count and return the number of vowels that exists in the string. Syntax function countVowels(str) { // Convert to lowercase for case-insensitive comparison // Loop through each character // Check if character is a vowel (a, e, i, o, u) // Return the count } Example: Using for Loop Following is the code − const ...
Read MoreConstruct an identity matrix of order n in JavaScript
An identity matrix is a square matrix where all diagonal elements are 1 and all other elements are 0. This type of matrix is fundamental in linear algebra and has the property that when multiplied with any matrix, it returns the original matrix unchanged. What is an Identity Matrix? An identity matrix of order n is an n × n square matrix where: All diagonal elements (where row index equals column index) are 1 All other elements are 0 For example, an identity matrix of order 3 will be: [ [1, ...
Read MoreConstructing largest number from an array in JavaScript
We need to write a JavaScript function that takes an array of numbers and arranges them to form the largest possible number by concatenating the digits. The key insight is that we can't simply sort numbers in descending order. For example, with numbers [3, 30], sorting gives [30, 3] → "303", but the correct answer is [3, 30] → "330". For example − If the input array is − const arr = [5, 45, 34, 9, 3]; Then the output should be − '954543' Algorithm The solution uses ...
Read MoreDigit sum upto a number of digits of a number in JavaScript
We are required to write a JavaScript function that takes in two numbers, let's say m and n as arguments. n will always be smaller than or equal to the number of digits present in m. The function should calculate and return the sum of first n digits of m. Problem Statement If the input numbers are: const m = 5465767; const n = 4; Then the output should be: 20 because 5 + 4 + 6 + 5 = 20 Solution Using String Conversion The most ...
Read MoreDifference between product and sum of digits of a number in JavaScript
We are required to write a JavaScript function that takes in a positive integer as the only argument. The function should first calculate the sum of the digits of the number and then their product. Finally, the function should return the absolute difference between the product and the sum. For example, if the input number is 12345: Sum of digits: 1 + 2 + 3 + 4 + 5 = 15 Product of digits: 1 × 2 × 3 × 4 × 5 = 120 Absolute difference: |120 - 15| = 105 Example ...
Read MoreGrouping array nested value while comparing 2 objects - JavaScript
When working with complex nested objects, you often need to compare and group data from different states. This article demonstrates how to group array nested values while comparing two objects in JavaScript. Problem Statement Suppose we have a JSON object containing "before" and "after" states of device data: const input = { "before": { "device": [ { "id": "1234", "price": "10", ...
Read MoreSort array of objects by string property value - JavaScript
Sorting an array of objects by a string property is a common task in JavaScript. The most efficient approach uses the built-in sort() method with localeCompare() for proper alphabetical ordering. Sample Data Let's work with this array of person objects: const people = [ { first_name: 'Lazslo', last_name: 'Jamf' }, { first_name: 'Pig', last_name: 'Bodine' }, { first_name: 'Pirate', last_name: 'Prentice' } ]; console.log("Original array:", people); Original array: [ { first_name: 'Lazslo', last_name: 'Jamf' }, { ...
Read MoreSmallest number of perfect squares that sums up to n in JavaScript
We need to write a JavaScript function that finds the minimum number of perfect squares that sum up to a given positive number. The function should find a combination of perfect square numbers (1, 4, 9, 16, 25, ...) which when added gives the input number, using as few perfect squares as possible. Problem Example If the input number is 123: Input: 123 Output: 3 Because 123 = 121 + 1 + 1 (using three perfect squares: 11², 1², 1²) Understanding the Pattern This is a classic Dynamic Programming problem. We ...
Read MoreSort an array to have specific items first in the array - JavaScript
When working with arrays of objects, you might need to sort them so that items with specific properties appear first. This is a common requirement in applications where you need to prioritize certain elements while maintaining the original order of others. Suppose we have an array of objects like this: const arr = [ {flag: true, other: 1}, {flag: true, other: 2}, {flag: false, other: 3}, {flag: true, other: 4}, {flag: true, other: 5}, ...
Read MoreCalculating a number from its factorial in JavaScript
We are required to write a JavaScript function that takes in a number as the only argument. The function should check whether there exists any number whose factorial is the number taken as input. If there exists any such number, we should return that number otherwise we should return -1. Problem Understanding Given a number, we need to find if it's a factorial of some integer. For example, 720 is the factorial of 6 because 6! = 6 × 5 × 4 × 3 × 2 × 1 = 720. If the input is: ...
Read More