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
Javascript Articles
Page 91 of 534
Constructing full name from first name, last name and optional middle name in JavaScript
We need to write a JavaScript function that takes in three strings: first name, last name, and an optional middle name, then returns the full name constructed from these inputs. Problem The challenge is handling the optional middle name parameter. If no middle name is provided, we should construct the full name with just the first and last names, avoiding extra spaces. Using Array Filter Method This approach uses an array to collect all name parts, filters out empty values, and joins them with spaces: const firstName = 'Vijay'; const lastName = 'Raj'; ...
Read MoreRealtime moving average of an array of numbers in JavaScript
A moving average calculates the average of elements from the start of an array up to each position. For each element at index i, we compute the average of elements from index 0 to i. Problem We need to write a JavaScript function that takes an array of numbers and returns a new array containing the cumulative moving average at each position. Input: [1, 2, 3, 4, 5] Output: [1, 1.5, 2, 2.5, 3] The first element is the average of just the first element (1/1 = 1). The second element is the average ...
Read MoreReducing array elements to all odds in JavaScript
We need to write a JavaScript function that transforms an array by converting all numbers to odd values. The transformation rules are: If the number is odd, leave it unchanged. If the number is even, subtract 1 from it to make it odd. This ensures all elements in the resulting array are odd numbers. Example Here's how to implement this transformation: const arr = [5, 23, 6, 3, 66, 12, 8]; const reduceToOdd = (arr = []) => { const res = []; ...
Read MoreAll right triangles with specified perimeter in JavaScript
We are required to write a JavaScript function that takes in a number that specifies the perimeter for a triangle. Our function should return an array of all the right triangle side triplets whose perimeter matches the specified input. Understanding Right Triangles A right triangle satisfies the Pythagorean theorem: a² + b² = c², where c is the hypotenuse (longest side). For a given perimeter P, we need a + b + c = P. Algorithm Approach We use three nested loops to check all possible combinations of triangle sides. For each triplet, we verify: ...
Read MoreReturn the nearest greater integer of the decimal number it is being called on in JavaScript
The Math.ceil() function returns the smallest integer greater than or equal to a given number. This means it "rounds up" to the nearest integer. Syntax Math.ceil(x) Parameters x: A number to round up to the nearest integer. Return Value Returns the smallest integer greater than or equal to the given number. If the input is not a number, returns NaN. Example: Basic Usage console.log(Math.ceil(4.3)); // 5 console.log(Math.ceil(4.8)); // 5 console.log(Math.ceil(5.0)); // 5 (already integer) console.log(Math.ceil(-2.3)); // -2 console.log(Math.ceil(-2.8)); // -2 ...
Read MoreFinding the only even or the only odd number in a string of space separated numbers in JavaScript
We are required to write a JavaScript function that takes in a string that contains numbers separated by spaces. The string either contains all odd numbers and only one even number or all even numbers and only one odd number. Our function should return that one different number from the string. Problem Given a string of space-separated numbers where all numbers are either odd or even except for one, we need to find and return that unique number that differs from the rest. Example Following is the code: const str = '2 4 ...
Read MoreReturning a decimal that have 1s in its binary form only at indices specified by array in JavaScript
Problem We are required to write a JavaScript function that takes in an array of unique non-negative integers. Our function should return a 32-bit integer such that the integer, in its binary representation, has 1 at only those indexes (counted from right) which are in the sequence. Example Following is the code − const arr = [1, 2, 0, 4]; const buildDecimal = (arr = []) => { const bitArr = Array(31).fill(0); let res = 0; arr.forEach(el => { ...
Read MoreReturning a range or a number that specifies the square root of a number in JavaScript
We need to write a JavaScript function that takes an integer n and returns either the exact square root (if n is a perfect square) or a range indicating where the square root falls between two consecutive integers. Problem Requirements Return integer k if n is a perfect square, such that k * k == n Return a range (k, k+1) if n is not a perfect square, where k * k < n and n < (k+1) * (k+1) Solution Approach We use Math.sqrt() to calculate the ...
Read MoreReturning the first number that equals its index in an array using JavaScript
Problem We need to write a JavaScript function that takes an array of numbers and returns the first number whose value equals its 0-based index position. This means we're looking for an element where array[i] === i. Example Following is the code − const arr = [9, 2, 1, 3, 6, 5]; const findFirstSimilar = (arr = []) => { for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(el === i){ ...
Read MoreCalculating value of a sequence based on some input using JavaScript
Problem We need to write a JavaScript function that takes a number n as input and calculates the value of a sequence term un. Given a sequence where: u1 = 0 and u2 = 2 Our function should find the value of un for which the following recurrence relation holds: 6unun+1 - 5unun+2 + un+1un+2 = 0 Understanding the Sequence From the recurrence relation, we can derive that this sequence follows the pattern un = 2n-1 for n ≥ 2, and u1 = 0. Example Implementation Here's the ...
Read More