Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 243 of 801
Why doesn't Postman get a "No 'Access-ControlAllow-Origin' header is present on the requested resource" error in JavaScript
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 MoreHow to set cookie value with AJAX request in JavaScript?
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 MoreFlattening a JSON object in JavaScript
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 MoreRemoving the odd occurrence of any number/element from an array in JavaScript
When working with arrays, you might need to remove elements that appear an odd number of times (excluding elements that appear only once). This tutorial shows how to implement this functionality in JavaScript. Problem Statement Given an array of numbers, we need to remove the last occurrence of any element that appears an odd number of times, but keep elements that appear only once. const arr = [1, 6, 3, 1, 3, 1, 6, 3]; console.log("Original array:", arr); Original array: [1, 6, 3, 1, 3, 1, 6, 3] In this example, ...
Read MoreComparing corresponding values of two arrays in JavaScript
When working with arrays in JavaScript, you might need to compare corresponding elements to determine which array has more "dominant" values at each position. This article demonstrates how to build a function that compares two arrays element by element and returns a result indicating which array has more greater values. Problem Statement Given two arrays of equal length, we need to compare corresponding elements and return: -1 if the first array has more elements greater than their corresponding elements in the second array 1 if the second array has more elements greater than their corresponding elements ...
Read MorePositive, negative and zeroes contribution of an array in JavaScript
In JavaScript, you may need to analyze an array of integers to find the fractional ratio of positive, negative, and zero values. This is useful for statistical analysis and data processing tasks. Problem Statement Given an array of integers containing positive, negative, and zero values: const arr = [23, -1, 0, 11, 18]; We need to write a function that calculates the fractional ratio for each group: negative, zero, and positive numbers. The output should be an array of three decimal values representing these ratios. For the above array with length 5, the ...
Read MoreSum excluding one element in JavaScript
In JavaScript, when you need to find the minimum and maximum possible sums by excluding exactly one element from an array, you can solve this efficiently using a single loop approach. The key insight is that to get the minimum sum (excluding one element), you remove the largest element. To get the maximum sum (excluding one element), you remove the smallest element. Problem Statement Given an array of integers, return an array with two values: First integer: smallest possible sum excluding any one element Second integer: greatest ...
Read MoreFinding desired sum of elements in an array in JavaScript
Finding groups of elements in an array that sum to a desired value is a common programming problem. This involves checking combinations of consecutive elements to match both a target sum and group size. Problem Statement Given an array of numbers, we need to find how many consecutive groups of a specific length sum up to a target value. For example: const arr = [1, 2, 1, 3, 2]; const sum = 3; const num = 2; We want to find groups of 2 consecutive elements that sum to 3. The groups [1, 2] ...
Read MoreFinding a pair that is divisible by some number in JavaScript
We need to write a JavaScript function that finds all pairs of numbers from an array whose sum is divisible by a given number. The function takes an array of numbers and a divisor as parameters. Problem Statement Given an array of numbers and a divisor, find all pairs where: (arr[i] + arr[j]) % divisor === 0, and i < j For example, with array [1, 2, 3, 4, 5, 6] and divisor 4, we need pairs whose sum is divisible by 4. Input and Expected Output Input: const arr = ...
Read MoreFinding matching pair from an array in JavaScript
We are required to write a JavaScript function that takes in an array of integers that might contain some repeating values. Our function should find out the number of pairs of identical integers we can extract out of the array. For example, if the input array is: const arr = [1, 5, 2, 1, 6, 2, 2, 9]; Then the output should be: const output = 2; because the desired pairs are (1, 1) and (2, 2). Note that we have three 2's, but we can only form one pair from ...
Read More