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 on Trending Technologies
Technical articles with clear explanations and examples
Nearest power 2 of a number - JavaScript
We need to write a JavaScript function that takes a number and returns the nearest power of 2. A power of 2 is any number that can be expressed as 2n where n is a whole number (1, 2, 4, 8, 16, 32, 64, 128, 256, etc.). For example, if the input is 365, the output should be 256 because 256 (28) is closer to 365 than the next power of 2, which is 512 (29). Algorithm Explanation The algorithm works by: Starting with base = 1 (20) Doubling the base until it exceeds the input ...
Read MoreStore count of digits in order using JavaScript
When working with strings containing digits, you often need to count how many times each digit appears. This is a common programming task that can be solved efficiently using JavaScript objects. Suppose we have a string with digits like this: const str = '11222233344444445666'; We need to write a JavaScript function that takes this string and returns an object representing the count of each digit in the string. For this string, the expected output should be: { "1": 2, "2": 4, "3": 3, "4": ...
Read MoreHow to perform Automated Unit Testing with JavaScript?
To perform automated unit testing in JavaScript, Unit.js is a cross-platform open-source unit testing framework that provides a simple and intuitive API for testing JavaScript applications. What is Unit.js? Unit.js is a JavaScript testing framework that works in both Node.js and browsers. It offers a fluent interface for writing readable test assertions and supports various testing patterns. Basic Syntax Unit.js uses a chainable syntax where you specify the data type and then chain assertion methods: test.dataType(value).assertionMethod(expectedValue); Simple String Testing Example Here's a basic example of testing a string value: ...
Read MoreFull page drag and drop files website with HTML
Creating a full-page drag and drop interface allows users to drop files anywhere on the webpage. This involves detecting drag events and showing a drop zone overlay. HTML Structure First, create the basic HTML with a drop zone overlay: #dropZone { position: fixed; top: 0; ...
Read MoreWhat is the importance of _.union() method in JavaScript?
The _.union() method from the Underscore.js library creates a new array containing unique values from multiple input arrays. It performs a union operation, removing duplicates while preserving the original order of first occurrence. Syntax _.union(array1, array2, ...arrayN); Parameters array1, array2, ...arrayN: Two or more arrays to be combined. The method accepts any number of arrays as arguments. Return Value Returns a new array containing all unique values from the input arrays, maintaining the order of first occurrence. Example with Numbers In the following example, _.union() combines multiple arrays and removes ...
Read MoreJavaScript BOM Window Screen
The JavaScript BOM (Browser Object Model) Window screen object provides detailed information about the user's display screen. This object is useful for creating responsive web applications and gathering display specifications. Screen Object Properties Property Description screen.width Returns the total screen width in pixels ...
Read MoreHow to set attribute in loop from array JavaScript?
Let's say we are required to write a function that takes in an array and changes the id attribute of first n divs present in a particular DOM according to corresponding values of this array, where n is the length of the array. We will first select all divs present in our DOM, iterate over the array we accepted as one and only argument and assign the corresponding id to each div. Basic Example Here's the HTML structure we'll work with: Setting Attributes in Loop ...
Read MoreUn-nesting array of object in JavaScript?
To un-nest an array of objects in JavaScript, you can use the map() method to restructure nested data into a flatter format. This is useful when you have deeply nested objects and want to extract specific properties to create a simpler structure. Problem: Nested Object Structure Let's say you have an array of student objects where subject details are nested inside each student: const studentDetails = [ { "studentId": 101, "studentName": "John", ...
Read MoreFinding area of triangle in JavaScript using Heron's formula
We are given the lengths of three sides of a triangle and we are required to write a function that returns the area of the triangle using the length of its sides. Heron's Formula We can calculate the area of a triangle if we know the lengths of all three sides, using Heron's formula: Step 1 − Calculate "s" (half of the triangle's perimeter): s = (a + b + c) / 2 Step 2 − Then calculate the Area using Heron's formula: A = √(s(s-a)(s-b)(s-c)) Syntax ...
Read MoreHow can I convert a string to boolean in JavaScript?
This tutorial teaches us to convert a string to a boolean in JavaScript. When developing applications, you might store boolean values in databases or local storage as strings. Later, you need to convert these string values back to boolean to perform specific operations. We'll explore three effective methods to convert strings to boolean values in JavaScript. Using the Comparison (==) and Ternary operator (? :) Using the Boolean constructor Using the Double Not (!!) Operator Using the Comparison (==) and Ternary operator (? ...
Read More