Front End Technology Articles

Page 189 of 652

Excluding extreme elements from average calculation in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 271 Views

We need to write a JavaScript function that calculates the average of array elements while excluding the smallest and largest values. This is useful when you want to remove outliers from statistical calculations. Problem Statement Given an array of numbers, calculate the average excluding the minimum and maximum values. This helps eliminate extreme values that might skew the results. Example Let's implement a function that finds the excluded average: const arr = [5, 3, 5, 6, 12, 5, 65, 3, 2]; const findExcludedAverage = arr => { const creds ...

Read More

Mathematics summation function in JavaScript

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

Problem We are required to write a JavaScript function that takes in a number n. Our function should return the sum of all the natural numbers from 1 to n including both 1 and n. Example Following is the code − const num = 34; const summation = (num = 1) => { let res = 0; for(let i = 1; i { return (num * (num + 1)) / 2; }; console.log(summationFormula(34)); // Same result console.log(summationFormula(100)); // Testing with larger number ...

Read More

Check for perfect square in JavaScript

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

We are required to write a JavaScript function that takes in a number and returns a boolean based on the fact whether or not the number is a perfect square. A perfect square is a number that can be expressed as the product of an integer with itself. For example, 16 is a perfect square because 4 × 4 = 16. Examples of Perfect Square Numbers 144 (12²), 196 (14²), 121 (11²), 81 (9²), 484 (22²) Method 1: Using Math.sqrt() The most straightforward approach is to find the square root and check if ...

Read More

Hours and minutes from number of seconds using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 484 Views

We are required to write a JavaScript function that takes in the number of seconds and returns the number of hours and minutes contained in those seconds. Problem Statement Given a number of seconds, we need to convert it into a readable format showing hours and minutes. Input: 3601 seconds Expected Output: "1 hour(s) and 0 minute(s)" Solution Here's how we can convert seconds to hours and minutes: const seconds = 3601; const toTime = (seconds = 60) => { const hR = 3600; // 1 hour ...

Read More

Removing first k characters from string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 236 Views

We are required to write a JavaScript function that takes in a string and a number, say k and returns another string with first k characters removed from the string. For example: If the original string is − const str = "this is a string" and, n = 4 then the output should be − const output = " is a string" Method 1: Using substr() Method The substr() method extracts characters from a string starting at a specified position. const str = 'this ...

Read More

Separating data type from array into groups in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 651 Views

In JavaScript, you often need to group array elements by their data types. This is useful for data analysis, validation, or organizing mixed-type arrays. We'll create a function that separates elements into groups based on their typeof result. Problem We need to write a JavaScript function that takes an array of mixed data types and returns an object where each key represents a data type and its value is an array containing all elements of that type. Solution Here's how to group array elements by their data types: const arr = [1, 'a', [], ...

Read More

Prime numbers within a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 770 Views

We are required to write a JavaScript function that takes in two numbers, say, a and b and returns the total number of prime numbers between a and b (including a and b, if they are prime). For example: If a = 21, and b = 38. The prime numbers between them are 23, 29, 31, 37 And their count is 4 Our function should return 4 Understanding Prime Numbers A prime number is a natural number greater than 1 that has ...

Read More

Even index sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 656 Views

We need to write a JavaScript function that calculates the sum of all integers at even indices (0, 2, 4, etc.) in an array, then multiplies this sum by the last element of the array. Problem Statement Given an array of integers, find the sum of elements at even indices and multiply by the last element. Input: [4, 1, 6, 8, 3, 9] Even indices: 0, 2, 4 → elements: 4, 6, 3 Sum: 4 + 6 + 3 = 13 Last element: 9 Result: 13 × 9 = 117 Solution const ...

Read More

ASCII sum difference of strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 694 Views

ASCII (American Standard Code for Information Interchange) is a 7-bit character encoding standard where every character has a unique decimal code. We can calculate the ASCII sum of strings and find their difference in JavaScript. Understanding ASCII Codes Each character has a specific ASCII value. For example, 'A' = 65, 'a' = 97, '0' = 48, and space = 32. We can get these values using JavaScript's charCodeAt() method. Getting ASCII Values console.log('A'.charCodeAt(0)); // 65 console.log('a'.charCodeAt(0)); // 97 console.log('0'.charCodeAt(0)); // 48 console.log(' '.charCodeAt(0)); ...

Read More

Sum of all positives present in an array in JavaScript

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

We are required to write a JavaScript function that takes in an array of numbers (positive and negative). Our function should calculate and return the sum of all the positive numbers present in the array. Method 1: Using reduce() with Helper Function This approach uses a helper function to check if a number is positive and reduce() to accumulate the sum: const arr = [5, -5, -3, -5, -7, -8, 1, 9]; const sumPositives = (arr = []) => { const isPositive = num => typeof num === 'number' && num ...

Read More
Showing 1881–1890 of 6,519 articles
« Prev 1 187 188 189 190 191 652 Next »
Advertisements