Javascript Articles

Page 368 of 534

Find the average of all elements of array except the largest and smallest - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 378 Views

We are required to write a JavaScript function that takes in an array of numbers and returns the average of its elements excluding the smallest and largest numbers. Approach The solution involves finding the minimum and maximum values, calculating the total sum, then computing the average of remaining elements after excluding min and max values. Example const arr = [1, 4, 5, 3, 5, 6, 12, 5, 65, 3, 2, 65, 9]; const findExcludedAverage = arr => { const creds = arr.reduce((acc, val) => { ...

Read More

Creating content with HTML5 Canvas much more complicated than authoring with Flash

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 190 Views

Flash provided amazing GUI tools and visual features for animations, allowing developers to build multimedia content within a self-contained platform. However, Flash required browser plugins and had security concerns that led to its deprecation. HTML5's element offers a modern, plugin-free alternative for creating graphics and animations using JavaScript. It provides direct access to a drawing context where you can render shapes, images, and complex animations programmatically. Basic Canvas Setup A canvas element requires only width and height attributes, plus standard HTML attributes: Drawing with Canvas Unlike Flash's visual timeline, Canvas ...

Read More

Reduce an array to the sum of every nth element - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 323 Views

We are required to write a JavaScript function that takes in an array of numbers and returns the cumulative sum of every number present at the index that is a multiple of n from the array. Let's write the code for this function − Example const arr = [1, 4, 5, 3, 5, 6, 12, 5, 65, 3, 2, 65, 9]; const num = 2; const nthSum = (arr, num) => { let sum = 0; for(let i = 0; i < arr.length; i++){ ...

Read More

Detect folders in Safari with HTML5

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 192 Views

Safari has limited support for folder detection compared to other browsers. When users drag and drop folders, Safari treats them differently than individual files, which can cause errors during file reading operations. The Problem with Folders in Safari When a folder is dropped onto a web page, Safari includes it in the files collection but cannot read its contents using FileReader. This causes the onerror event to trigger, which we can use to detect folder drops. Folder Detection Method The following code attempts to read each dropped item as a file. If it's a folder, the ...

Read More

Program to append two given strings such that, if the concatenation creates a double character then omit one of the characters - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 232 Views

We are required to write a JavaScript function that takes in two strings and concatenates the second string to the first string. If the last character of the first string and the first character of the second string are the same then we have to omit one of those characters. Let's say the following are our strings in JavaScript − Problem Example const str1 = 'Food'; const str2 = 'dog'; // Expected output: 'Foodog' (last 'd' of 'Food' matches first 'd' of 'dog') Solution Let's write the code for this function − ...

Read More

Is HTML5 canvas and image on polygon possible?

George John
George John
Updated on 15-Mar-2026 478 Views

Yes, it is possible to draw images on polygons in HTML5 canvas. You can achieve this by creating a pattern from an image and applying it as a fill, or by using transformation matrices to manipulate how the image is drawn within the polygon shape. Method 1: Using Image Patterns Create a pattern using the image object and set it as the fillStyle for your polygon: const canvas = document.getElementById('myCanvas'); const context = canvas.getContext("2d"); // Create an image object const img = new Image(); img.onload = function() { ...

Read More

Prime numbers in a range - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ 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 = 2, and b = 21, the prime numbers between them are 2, 3, 5, 7, 11, 13, 17, 19 And their count is 8. Our function should return 8. Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 ...

Read More

Stop Web Workers in HTML5

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 1K+ Views

Web Workers allow for long-running scripts that are not interrupted by scripts that respond to clicks or other user interactions and allows long tasks to be executed without yielding to keep the page responsive. Web Workers don't stop by themselves but the page that started them can stop them by calling the terminate() method. Syntax worker.terminate(); Example: Creating and Terminating a Web Worker First, let's create a simple Web Worker script (worker.js): // worker.js self.onmessage = function(e) { let count = 0; while ...

Read More

Comparing forEach() and reduce() for summing an array of numbers in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 494 Views

When summing arrays in JavaScript, both forEach() and reduce() are common approaches. This article compares their performance to help you choose the right method. Since we can't demonstrate with truly massive arrays here, we'll simulate the performance impact by running the summing operation many times in a loop. The Two Approaches Here's how each method works for summing an array: const arr = [1, 4, 4, 54, 56, 54, 2, 23, 6, 54, 65, 65]; // Using reduce() - functional approach const reduceSum = arr => arr.reduce((acc, val) => acc + val); // ...

Read More

MediaStream in HTML5

Daniol Thomas
Daniol Thomas
Updated on 15-Mar-2026 421 Views

The MediaStream represents synchronized streams of media content in HTML5. It provides access to audio and video tracks from sources like microphones and webcams. When there are no audio tracks, it returns an empty array, and for video streams, if a webcam is connected, stream.getVideoTracks() returns an array containing MediaStreamTrack objects representing the webcam stream. Getting User Media Access The navigator.mediaDevices.getUserMedia() method is the modern way to access media streams. It returns a Promise that resolves with a MediaStream object: Start Audio async function startAudio() { try { ...

Read More
Showing 3671–3680 of 5,340 articles
« Prev 1 366 367 368 369 370 534 Next »
Advertisements