Javascript Articles

Page 348 of 534

Wildcard matching of string JavaScript

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

We are required to write a JavaScript function that accepts two strings and a number n. The function matches the two strings i.e., it checks if the two strings contains the same characters. The function should return true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, otherwise the function should return false. Let's write the code for this function — Example const str1 = 'first string'; const str2 = 'second string'; const wildcardMatching = (first, second, num) => { ...

Read More

Does HTML5 Canvas support Double Buffering?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 676 Views

HTML5 Canvas doesn't have built-in double buffering, but you can implement it manually by creating a second off-screen canvas. This technique prevents flickering during complex animations by drawing to a hidden canvas first, then copying the complete frame to the visible canvas. What is Double Buffering? Double buffering uses two canvases: a visible "front buffer" and an invisible "back buffer". You draw your entire frame on the back buffer, then copy it to the front buffer in one operation. This eliminates the visual artifacts that occur when drawing directly to the visible canvas. Implementation Create an ...

Read More

Separate odd and even in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 821 Views

In JavaScript, separating odd and even numbers means rearranging an array so that all even numbers appear before all odd numbers. This is commonly achieved using custom sorting functions or array methods like filter(). Using Custom Sort Function The most efficient approach uses Array.sort() with a custom comparator that prioritizes even numbers: const arr = [2, 6, 3, 7, 8, 3, 5, 4, 3, 6, 87, 23, 2, 23, 67, 4]; const isEven = num => num % 2 === 0; const sorter = (a, b) => { if(isEven(a) && !isEven(b)){ ...

Read More

Can you take a screenshot of the page using HTML5 Canvas?

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

Html2Canvas is a JavaScript library that can take screenshots of web pages or specific elements. It doesn't capture actual screenshots but recreates the visual representation based on DOM information and CSS styles. How Html2Canvas Works The library reads the DOM structure and CSS properties to generate a canvas representation of the page. This approach works entirely in the browser without requiring server-side processing or special permissions. Basic Example Here's a complete example showing how to capture a screenshot of a specific container: ...

Read More

JavaScript Return an array that contains all the strings appearing in all the subarrays

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 339 Views

We have an array of arrays like this − const arr = [ ['foo', 'bar', 'hey', 'oi'], ['foo', 'bar', 'hey'], ['foo', 'bar', 'anything'], ['bar', 'anything'] ] We are required to write a JavaScript function that takes in such array and returns an array that contains all the strings which appears in all the subarrays. Let's write the code for this function Example const arr = [ ['foo', 'bar', 'hey', 'oi'], ['foo', 'bar', 'hey'], ...

Read More

How to find the size of localStorage in HTML?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 731 Views

localStorage is used to persist information across multiple sessions. It has a maximum size of 5MB in most browsers and stores data as key-value pairs. Understanding localStorage Size Calculation Each character in localStorage is stored as UTF-16, taking 2 bytes. To calculate the size, we multiply the string length by 2 and convert to MB. Example: Calculate localStorage Size You can try to run the following code snippet to check the size allocated: localStorage Size Calculator localStorage Size Breakdown ...

Read More

How to split last n digits of each value in the array with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 365 Views

We have an array of mixed values like numbers and strings, and we want to extract the last n digits from each element that has enough characters. const arr = ["", 20191219, 20191220, 20191221, 20191222, 20191223, 20191224, 20191225]; We need to write a JavaScript function that takes this array and a number n. If an element contains more than or equal to n characters, the function should return only the last n characters. Otherwise, the element should remain unchanged. Solution Here's how we can implement this function using the map() method and string manipulation: ...

Read More

How to draw an oval in HTML5 canvas?

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

Drawing an oval in HTML5 canvas can be achieved by scaling a circle. Since canvas doesn't have a built-in oval method, we use the scale() transformation to stretch a circle into an elliptical shape. How It Works The technique involves three steps: Save the current canvas state with save() Apply scaling transformation to stretch the circle Draw a circle using arc(), which appears as an oval due to scaling Restore the original state with restore() Example HTML5 Canvas Oval ...

Read More

How to check whether multiple values exist within a JavaScript array

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

We are required to write a JavaScript function that takes in two arrays of Numbers and checks whether all the elements of the first array exist in the second or not. Following are our arrays − const arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89]; Let's write the code and check for multiple values − Using indexOf() Method const arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89]; const contains = (first, second) => { const ...

Read More

How to apply antialiasing in HTML5 canvas drawImage()?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 2K+ Views

HTML5 Canvas provides built-in antialiasing through image smoothing properties to improve the quality of scaled images rendered with drawImage(). Setting Image Smoothing Quality The primary way to control antialiasing is through the imageSmoothingQuality property: const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); // Set antialiasing quality ctx.imageSmoothingQuality = "high"; // "low", "medium", or "high" // Create a small test image const img = new Image(); img.onload = function() { // Draw scaled image with antialiasing ctx.drawImage(img, 0, 0, 200, 150); }; img.src = ...

Read More
Showing 3471–3480 of 5,340 articles
« Prev 1 346 347 348 349 350 534 Next »
Advertisements