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 330 of 840
How to add content in section using jQuery/JavaScript?
Adding content to the section dynamically is useful for loading CSS, JavaScript, or meta tags after page load. jQuery and JavaScript provide several methods to accomplish this task programmatically. There are three main approaches to add content to the head section: Using jQuery's .append() method Using JavaScript's document.createElement() method Using JavaScript's insertAdjacentHTML() method Let's explore each approach with working examples. Using jQuery's .append() Method jQuery's append() method provides a simple way to add content to the head section: ...
Read MoreFinding whether a number is triangular number in JavaScript
Triangular numbers are sequences of numbers that can form an equilateral triangle. The nth triangular number is the sum of the first n natural numbers, calculated as T(n) = n × (n + 1) / 2. Triangular Numbers Pattern: T(1) = 1 T(2) = 3 ...
Read MoreJust smaller number with monotone digits in JavaScript
In JavaScript, finding the largest number with monotonically increasing digits that is less than or equal to a given number is a common algorithmic problem. A number has monotonically increasing digits when each digit is greater than or equal to the previous digit. What are Monotonically Increasing Digits? An integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x ≤ y. For example, 1234 and 1139 have monotone increasing digits, while 332 does not. Problem Statement We need to write a JavaScript function that takes a number ...
Read MorePicking the odd one out in JavaScript
We are required to write a JavaScript function that takes in an array of literals that contains all similar elements but one. Our function should return the unlike number. Therefore, let's write the code for this function − Example The code for this will be − const arr = [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]; // considering that the length of array is at least 3 const findUnlike = arr => { for(let i = 1; i < arr.length-1; i++){ ...
Read MoreConverting miles per gallon to kilometer per liter in JavaScript
Converting miles per gallon (MPG) to kilometers per liter (km/L) is a common unit conversion in JavaScript applications dealing with fuel efficiency across different measurement systems. Understanding the Conversion To convert MPG to km/L, we need two conversion factors: 1 mile = 1.609344 kilometers 1 gallon = 4.54609188 liters (Imperial gallon) The conversion formula is: km/L = MPG × (1.609344 ÷ 4.54609188) Example Implementation const num = 25; const converter = (mpg) => { let LITRES_PER_GALLON = 4.54609188; let KILOMETERS_PER_MILE = 1.609344; ...
Read MoreFinding state after all collisions in JavaScript
Problem We are required to write a JavaScript function that takes in an array, arr, that represents the positions of different asteroids in a one-dimensional space. For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed. Our function is supposed to find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet. Example Input ...
Read MoreHow does Implicit coercion differ from Explicit coercion in JavaScript?
In this article, you will understand how implicit coercion differs from explicit coercion in JavaScript. An implicit coercion is an automatic conversion of values from one datatype to another that JavaScript performs automatically without programmer intervention. An explicit coercion is the deliberate conversion of data type using built-in functions or operators. Implicit Coercion JavaScript automatically converts data types when needed, especially in operations involving different types. let number = 5; let text = "10"; // JavaScript automatically converts number to string let result1 = number + text; console.log("5 + '10' =", result1, typeof result1); ...
Read MorePerforming the subtraction operation without the subtraction operator in JavaScript
We are required to write a JavaScript function that takes in two numbers and returns their difference but without using the (-) sign. This problem can be solved using bitwise operations. The key insight is that subtraction can be performed using XOR (^) and AND (&) operations combined with bit shifting. How It Works The algorithm uses these bitwise operations: XOR (^): Performs subtraction without handling borrows AND (&): Identifies positions where borrowing is needed Left shift ( { console.log(`Step ${step}: a=${a}, b=${b}`); if(b === 0){ console.log(`Result: ${a}`); return a; } const xor = a ^ b; const borrow = (~a & b)
Read MoreConverting a string to NATO phonetic alphabets in JavaScript
The NATO phonetic alphabet is a standardized set of code words used to represent letters of the alphabet in radio and telephone communications. This tutorial shows how to convert any string into NATO phonetic alphabet representation using JavaScript. NATO Phonetic Alphabet The 26 code words are: Alfa, Bravo, Charlie, Delta, Echo, Foxtrot, Golf, Hotel, India, Juliett, Kilo, Lima, Mike, November, Oscar, Papa, Quebec, Romeo, Sierra, Tango, Uniform, Victor, Whiskey, X-ray, Yankee, Zulu. Implementation Here's a complete function that converts a string to NATO phonetic alphabet: const str = 'this is simple string'; const ...
Read MoreReducing array elements to all odds in JavaScript
We need to write a JavaScript function that transforms an array by converting all numbers to odd values. The transformation rules are: If the number is odd, leave it unchanged. If the number is even, subtract 1 from it to make it odd. This ensures all elements in the resulting array are odd numbers. Example Here's how to implement this transformation: const arr = [5, 23, 6, 3, 66, 12, 8]; const reduceToOdd = (arr = []) => { const res = []; ...
Read More