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 486 of 840
Generate random characters and numbers in JavaScript?
Generating random characters and numbers is a common requirement in JavaScript applications. You can create random strings by combining characters from a predefined set using Math.random(). Setting Up Character Pool First, define a string containing all possible characters you want to include: var storedCharacters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; Basic Random String Generator Here's a function that generates random strings of any specified length: function generateRandomString(size) { var generatedOutput = ''; var storedCharacters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; var totalCharacterSize = storedCharacters.length; ...
Read MoreCalculate difference between circumference and area of a circle - JavaScript
In this tutorial, we'll learn how to calculate the difference between the area and circumference of a circle using JavaScript. This is a common mathematical problem that demonstrates basic geometry calculations and JavaScript functions. Circle Formulas Before we start coding, let's review the formulas: Area of a circle: π × r² Circumference of a circle: 2 × π × r Where r is the radius of the circle and π (pi) is approximately 3.14159. JavaScript Implementation We'll create a function that takes the radius as input and returns the absolute difference between area ...
Read MoreFat vs concise arrow functions in JavaScript
Arrow functions in JavaScript come in two forms: fat arrow (with curly braces) and concise arrow (without curly braces). The concise form is a streamlined syntax for single-expression functions that provides implicit return functionality. Syntax Comparison Fat arrow function with explicit return: let add = (a, b) => { return a + b; } Concise arrow function with implicit return: let add = (a, b) => a + b; Single parameter (parentheses optional): let square = x => x * x; No parameters (parentheses required): ...
Read MoreSmallest positive value that cannot be represented as sum of subarray JavaScript
We have a sorted array of positive integers like this: const arr = [1, 3, 6, 10, 11, 15]; We are required to write a function, say findSmallest() that takes in one such array and returns the smallest positive integer that cannot be represented as the sum of some subarray of this original array. For example, for the array written above, 2 is the smallest positive integer which cannot be reached by summing any subarray of this original array. Algorithm Approach Since the array is sorted, we can achieve the solution to this ...
Read MoreIteration of for loop inside object in JavaScript to fetch records with odd CustomerId?
In JavaScript, you can iterate through an array of objects using a for loop and filter records based on specific conditions. Here's how to fetch records with odd CustomerId values. Array of Objects Structure Let's start with an array containing customer objects: var customerDetails = [ { customerId: 101, customerName: "John" }, { customerId: 102, ...
Read MoreInserting element at falsy index in an array - JavaScript
We are required to write an Array function, let's say, pushAtFalsy() The function should take in an array and an element. It should insert the element at the first falsy index it finds in the array. If there are no empty spaces, the element should be inserted at the last of the array. We will first search for the index of empty position and then replace the value there with the value we are provided with. Understanding Falsy Values In JavaScript, falsy values include: null, undefined, false, 0, "" (empty string), and NaN. However, since 0 ...
Read MorePseudo mandatory parameters in JavaScript
Pseudo mandatory parameters in JavaScript allow you to enforce that certain function parameters must be provided by the caller. While JavaScript doesn't have built-in mandatory parameters like some other languages, you can simulate this behavior using various techniques. What are Pseudo Mandatory Parameters? Pseudo mandatory parameters are function parameters that appear optional syntactically but will throw an error if not provided. This helps catch bugs early and makes your function's requirements explicit. Method 1: Using a Helper Function The most common approach is to create a helper function that throws an error when called: ...
Read MoreRemove all characters of first string from second JavaScript
When working with strings in JavaScript, you may need to remove all characters from one string that appear in another string. This is useful for filtering, data cleaning, or text processing tasks. Let's say we have two strings with characters in no specific order. We need to write a function that takes these two strings and returns a modified version of the second string with all characters from the first string removed. Example Strings const first = "hello world"; const second = "hey there"; Using Array Methods (Recommended) The most straightforward approach uses ...
Read MoreGet value of any attribute from XML data in JavaScript?
To get the value of any attribute from XML data in JavaScript, you can use jQuery's attr() method or native JavaScript methods like getAttribute(). This is useful when working with XML strings or DOM elements that contain XML data. Using jQuery's attr() Method The attr() method allows you to extract attribute values from XML elements wrapped in jQuery objects: XML Attribute Example ...
Read MoreTrigger a button click and generate alert on form submission in JavaScript
In JavaScript, you can trigger a button click event and generate an alert on form submission using event handlers. This tutorial demonstrates how to attach a click event listener to a button and display an alert when the form is submitted. HTML Structure First, let's create a basic HTML form with a button: Submit JavaScript Event Handler Using jQuery, you can attach a click event handler to the button based on its ID: $("#submitForm").click(function() { alert("The Form has been Submitted."); }); Complete Example Here's ...
Read More