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
Articles by AmitDiwan
Page 424 of 840
Positive integer square array JavaScript
In JavaScript, we often need to filter arrays to extract specific types of numbers and perform operations on them. This example demonstrates how to find positive integers in an array and return the sum of their squares. Problem Statement Given an array containing various numbers (positive, negative, decimals, and integers), we need to write a function that returns the sum of squares of all positive integers only. Solution We'll use the reduce() method to iterate through the array and accumulate the sum of squares for positive integers: const arr = [1, -4, 6.1, 0.1, ...
Read MoreParse JSON in JavaScript to display a specific name/value pair?
To parse JSON in JavaScript, you can use the native JSON.parse() method or jQuery's parseJSON() function. To display specific name/value pairs, you can use array methods like forEach() or jQuery's $.each() function. Using Native JSON.parse() The modern approach uses the built-in JSON.parse() method: Parse JSON Example const APIData = '[{"Name":"John", "Age":21}, {"Name":"David", "Age":24}, {"Name":"Bob", ...
Read MoreSum of prime numbers between a range - JavaScript
We are required to write a JavaScript function that takes in two numbers, say a and b and returns the sum of all the prime numbers that fall between a and b. We should include a and b if they are prime as well. Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples include 2, 3, 5, 7, 11, 13, etc. Algorithm Overview To solve this problem, we need two functions: isPrime() - checks if a ...
Read MoreExplain Strict Comparison in JavaScript switch statement?
The JavaScript switch statement performs strict comparison (===) to match values. This means both value and type must be identical for a case to execute. If no strict match is found, the default case runs. Understanding Strict Comparison Strict comparison checks both value and type. For example, the number 1 is not equal to the string "1" in strict comparison: Strict Comparison Demo // Demonstrating strict vs loose comparison ...
Read MoreMake numbers in array relative to 0 – 100 in JavaScript
Let's say, we have an array that contains some numbers, our job is to write a function that takes in the array and maps all the values relative to 0 to 100. This means that the greatest number should get replaced by 100, the smallest by 0, and all others should get converted to specific numbers between 0 and 100 according to the ratio. Understanding the Formula To normalize values to a 0-100 scale, we use the formula: normalizedValue = ((value - min) / (max - min)) * 100 This formula ensures the smallest ...
Read MoreHow do I display only the visible text with jQuery?
To display only the visible text in jQuery, use the :visible selector. This selector targets elements that are currently visible on the page (not hidden with display:none, visibility:hidden, or other hiding methods). Syntax $(selector).filter(':visible') $(selector + ':visible') $(':visible') Example Visible Text with jQuery Hidden Text Visible Text ...
Read MoreFetch values by ignoring a specific one in JavaScript?
To ignore a specific value when fetching data from an array or object, use the logical NOT (!) operator in conditional statements to exclude unwanted values and retrieve only the ones you need. Using NOT Operator with Array of Objects The most common approach is to loop through your data and use the inequality operator (!=) to skip specific values: var customerDetails = [ { customerName: "John", customerAge: 28, customerCountryName: "US" }, ...
Read MoreDigit distance of two numbers - JavaScript
We are required to write a JavaScript function that takes in two numbers and returns their digit distance. What is Digit Distance? The digit distance of two numbers is the absolute sum of the difference between their corresponding digits from left to right. For example, if the numbers are: 345 678 Then the digit distance calculation will be: |3-6| + |4-7| + |5-8| = 3 + 3 + 3 = 9 Implementation Here's how to calculate digit distance in JavaScript: const num1 = 345; const num2 ...
Read MoreExplain JavaScript Regular Expression modifiers with examples
JavaScript regular expression modifiers are optional flags that modify how the pattern matching behaves. These modifiers enable features like case-insensitive matching, global searching, and multiline matching. Available Modifiers Modifier Name Description ...
Read MoreHow to combine 2 arrays into 1 object in JavaScript
Let's say, we have two arrays of equal lengths and are required to write a function that maps the two arrays into an object. The corresponding elements of the first array becomes the corresponding keys of the object and the elements of the second array become the value. We will reduce the first array, at the same time accessing elements of the second array by index. This is a common pattern for creating objects from parallel arrays. Using Array.reduce() Method const keys = [ 'firstName', 'lastName', 'isEmployed', ...
Read More