Front End Technology Articles

Page 179 of 652

Return Vowels in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 850 Views

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 More

Construct an identity matrix of order n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 981 Views

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 More

Constructing largest number from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 613 Views

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 More

Digit sum upto a number of digits of a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 521 Views

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 More

Difference between product and sum of digits of a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 354 Views

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 More

Smallest number of perfect squares that sums up to n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 341 Views

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 More

Calculating a number from its factorial in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 240 Views

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

Grouping array nested value while comparing 2 objects - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 470 Views

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 More

Sort array of objects by string property value - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 386 Views

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 More

Checking if one string can be achieved from another with single tweak in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 155 Views

We are required to write a JavaScript function that takes in two strings of characters lets call them str1 and str2. The function should check whether we can form str2 from str1 by deleting exactly one character from str1. If we can do so the function should return true, false otherwise. For example − If the input strings are − const str1 = 'chemistty'; const str2 = 'chemisty'; Then the output should be − const output = true; Approach The solution involves checking if the length difference is ...

Read More
Showing 1781–1790 of 6,519 articles
« Prev 1 177 178 179 180 181 652 Next »
Advertisements