Javascript Articles

Page 81 of 534

Calculate the hypotenuse of a right triangle in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 788 Views

We are required to write a JavaScript function that takes in two numbers. The first number represents the length of the base of a right triangle and the second is perpendicular. The function should then compute the length of the hypotenuse based on these values. For example − If base = 8, perpendicular = 6 Then the output should be 10 The Pythagorean Theorem The hypotenuse is calculated using the Pythagorean theorem: c² = a² + b², where c is the hypotenuse and a, b are the other two sides. ...

Read More

JavaScript function that generates all possible combinations of a string

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 835 Views

We are required to write a JavaScript function that takes in a string as the only argument. The function should generate an array of strings that contains all possible combinations of characters from the string. Understanding the Problem The function uses a bitwise approach to generate all possible combinations. It first extracts individual characters, then uses binary representation to determine which characters to include in each combination. Example Following is the code − const str = 'Delhi'; const allCombinations = (str1 = '') => { const arr = []; ...

Read More

Return Vowels in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 832 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 946 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 602 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 504 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 317 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 320 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 213 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

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 141 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 801–810 of 5,340 articles
« Prev 1 79 80 81 82 83 534 Next »
Advertisements