Web Development Articles

Page 190 of 801

Excluding extreme elements from average calculation in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 270 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

Returning number with increasing digits. in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 215 Views

We are required to write a JavaScript function that takes in a number n. Our function should return it with its digits in descending order. Essentially, we should rearrange the digits to create the highest possible number. Problem Given a number, we need to arrange its digits in descending order to form the largest possible number from those digits. Solution The approach is to convert the number to a string, split it into individual digits, sort them in descending order, and then convert back to a number. Example const num = 5423267; ...

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

Deep count of elements of an array using JavaScript

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

We are required to write a JavaScript function that takes in a nested array of elements and returns the deep count of all elements present in the array, including those in nested subarrays. Problem Given a nested array, we need to count all elements at every level of nesting. Input const arr = [1, 2, [3, 4, [5]]]; Output const output = 7; Because the elements at level 1 are 2, elements at level 2 are 2 and elements at level 3 are 1. Hence the deep count is 7. Using Recursive Reduce Method ...

Read More

Removing first k characters from string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 224 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

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

Prime numbers within a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 761 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

Hours and minutes from number of seconds using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 479 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

ASCII sum difference of strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 691 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

Separating data type from array into groups in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 647 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
Showing 1891–1900 of 8,010 articles
« Prev 1 188 189 190 191 192 801 Next »
Advertisements