Javascript Articles

Page 144 of 534

Finding the Largest Triple Product Array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 223 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument. Based on the array taken in as input, the function should construct a new array of the same length based on the following criteria. Any corresponding element of the output array should be the product of the three largest numbers encountered thus far. If the corresponding index is less than 3 (we have not encountered three elements yet) then the corresponding value should be -1. Although we can use non-unique values to calculate the product, those non-unique values should ...

Read More

Counting the number of 1s upto n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 312 Views

Counting the number of 1s from 1 to n is a common algorithmic problem in JavaScript. We need to count how many times the digit "1" appears in all positive integers up to and including n. For example, if n = 31, the digit "1" appears in: 1, 10, 11 (twice), 12, 13, 14, 15, 16, 17, 18, 19, 21, 31. That's a total of 14 occurrences. Problem Analysis The challenge is to efficiently count digit occurrences without iterating through every number. We can solve this using digit-by-digit analysis or a simpler brute force approach. Method ...

Read More

Finding hamming distance in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 675 Views

The Hamming distance between two strings of equal length is the number of positions at which the corresponding characters differ. It measures the minimum number of single-character edits needed to transform one string into another. In JavaScript, we can calculate Hamming distance by comparing each character position and counting the differences. This metric is commonly used in coding theory, cryptography, and bioinformatics. Basic Implementation Here's a JavaScript function that calculates the Hamming distance between two strings: const str1 = 'Hello World'; const str2 = 'Heeyy World'; const findHammingDistance = (str1 = '', str2 = ...

Read More

Grouping words with their anagrams in JavaScript

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

Two words or phrases which can be made by arranging the letters of each other in a different order are called anagrams of each other, like "rat" and "tar". We need to write a JavaScript function that takes in an array of strings that might contain some anagram strings. The function should group all the anagrams into separate subarrays and return the new array thus formed. Problem Example If the input array is: const arr = ['rat', 'jar', 'tar', 'raj', 'ram', 'arm', 'mar', 'art']; Then the output array should be: const ...

Read More

Finding the group with largest elements with same digit sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 185 Views

We are required to write a JavaScript function that takes in a positive integer, say n, as the only argument. The function should first group the integers from 1 to n into subarrays where each subarray contains all elements with the same digit sum. Then the function should examine each subarray and return the length of the largest group (the group with the most elements). Understanding Digit Sum The digit sum is calculated by adding all digits of a number. For example: Digit sum of 15 = 1 + 5 = 6 Digit sum of 23 ...

Read More

Write a program in JavaScript to check if two strings are anagrams of each other or not

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 15-Mar-2026 23K+ Views

Given two strings 'a' and string 'b', we have to check if they are anagrams of each other or not and return True/False. Two strings are anagrams if they contain the same characters with the same frequency, just rearranged. Input-1 − String a = "india" String b = "nidia" Output − True Explanation − Since the given string 'b' contains all the characters in the string 'a' with the same frequency, we will return True. Input-2 − String a = "hackathon" String b = "achcthoon" Output ...

Read More

How to take screenshot of a div with JavaScript

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

Taking a screenshot of a div element in JavaScript requires converting HTML elements to an image format. Since JavaScript cannot directly capture DOM elements as images, we use third-party libraries like html2canvas to achieve this functionality. The html2canvas library renders DOM elements onto a canvas element, which can then be converted to an image or downloaded. This approach works by parsing the CSS styles and creating a visual representation of the element. Installation and Setup To use html2canvas, include it via CDN or install it through npm: // Via CDN (add to HTML head) ...

Read More

Why doesn't Postman get a "No 'Access-ControlAllow-Origin' header is present on the requested resource" error in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 787 Views

Understanding the CORS Difference When making network requests to a remote server with a different origin, web browsers enforce CORS (Cross-Origin Resource Sharing) policies and show "No 'Access-Control-Allow-Origin' header" errors. However, tools like Postman can successfully make the same requests without encountering these CORS restrictions. The Browser Environment Problem Web browsers implement the Same-Origin Policy as a security measure. When JavaScript code running in a browser tries to make a request to a different domain, protocol, or port, the browser blocks the request before it reaches the server: // This will likely cause a ...

Read More

How to set cookie value with AJAX request in JavaScript?

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

When making AJAX requests in JavaScript, you often need to send cookies to the server for authentication or session management. Fortunately, modern browsers automatically include cookies in AJAX requests to the same domain, but you need to set them properly first. How Cookies Work with AJAX AJAX requests automatically send all relevant cookies to the server without additional configuration. The key is setting the cookie correctly using JavaScript's document.cookie property before making your AJAX call. Setting a Basic Cookie Here's how to set a ...

Read More

Flattening a JSON object in JavaScript

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

Flattening a JSON object means converting a nested object structure into a single-level object where nested keys are represented using dot notation. This technique is useful for data processing, form handling, and API transformations. Suppose we have the following JSON object that may contain nesting up to any level: const obj = { "one": 1, "two": { "three": 3 }, "four": { "five": 5, "six": { "seven": 7 ...

Read More
Showing 1431–1440 of 5,340 articles
« Prev 1 142 143 144 145 146 534 Next »
Advertisements