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
Expanding binomial expression using JavaScript
We need to write a JavaScript function that expands binomial expressions of the form (ax+b)^n, where a and b are integers, x is a variable, and n is a natural number. The function returns the expanded polynomial as a string. Problem Statement Given an expression like (8a+6)^4, we need to expand it using the binomial theorem. The result should be in the form ax^b+cx^d+ex^f... with coefficients and powers in decreasing order. Understanding the Binomial Theorem The binomial theorem states that (x+y)^n = Σ(C(n, k) * x^(n-k) * y^k) where C(n, k) is the binomial coefficient. ...
Read MoreHow to set cursor position in content-editable element using JavaScript?
In HTML, content editable div allows users to edit the content of the div element. We need to pass the contenteditable attribute with true Boolean value to the div element to make any div element editable. The content editable div contains the caret cursor by default, and sometimes we may require to set the caret cursor position in the content editable div element to edit the content of the div. However, we can set the caret cursor position anywhere by clicking at a particular place in the content editable div. This tutorial will teach us to use different ...
Read MoreJavaScript - Check if value is a percentage?
To check the value for percentage, use a regular expression. When dealing with user input or processing data in JavaScript, it's important to make sure a value is a valid percentage. This article shows you how to check if a value is a percentage using JavaScript, highlighting a useful method that involves regular expressions. Let's say the following is our value: var value = "97%"; To check the value for percentage, we are using a regular expression that matches numbers followed by the % symbol. Using Regular Expression The best way to check ...
Read MoreFilter array based on another array in JavaScript
In this article, we are going to learn how to filter an array based on another array in JavaScript. An Array in JavaScript is used to store different elements. These elements are stored at contiguous memory locations. By using index numbers, we can access any or each data element present in the array. Index numbers start from 0. Syntax Following is the syntax of the array in JavaScript – const array_name = [item1, item2, ...]; The following is a simple declaration of array in JavaScript: const colors = ['Blue', 'Limegreen', 'Orange', ...
Read MoreJavaScript Reverse the order of the bits in a given integer
We are required to write a JavaScript program that reverses the order of the bits in a given integer. For example − 56 -> 111000 after reverse 7 -> 111 Another example, 234 -> 11101010 after reverse 87 -> 1010111 How It Works The algorithm converts the number to binary, reverses the binary string, and converts back to decimal: Convert number to binary string using toString(2) Split into array, reverse, and join back Parse reversed binary string to decimal using parseInt(reversedBinary, 2) Example const ...
Read MoreGet the max n values from an array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n, as the second argument. Our function should then pick the n greatest numbers from the array and return a new array consisting of those numbers. Using Sort and Slice The most straightforward approach is to sort the array in descending order and take the first n elements: const arr = [3, 4, 12, 1, 0, 5, 22, 20, 18, 30, 52]; const pickGreatest = (arr = [], num = 1) ...
Read MoreFinding the longest Substring with At Least n Repeating Characters in JavaScript
We are required to write a JavaScript function that takes in a string as the first argument and a positive integer n as the second argument. The string is likely to contain some repeating characters. The function should find out and return the length of the longest substring from the original string in which all characters appear at least n number of times. For example, if the input string is 'kdkddj' and n is 2, the output should be 5 because the desired longest substring is 'kdkdd'. Problem Understanding Given a string and a number n, ...
Read MoreHow to set cursor style to pointer for links without href?
We can use the tag in HTML to add a link to the web page. The default cursor style for the elements is the pointer, but removing the href attribute from the tag changes the cursor style. So, in this tutorial, we will learn to keep the cursor style to pointer for tags without the href attribute. Users can follow the example below to check out the default cursor style for the elements. Default Behavior Example In the example below, we have used the tag to create three different links. ...
Read MoreLooping numbers with object values and push output to an array - JavaScript?
In JavaScript, you can create an array from an object by mapping object values to specific positions using Array.from(). This technique is useful when you have sparse data that needs to be converted into a dense array format. Problem Overview Given an object with numeric keys and values, we want to create an array where the object values are placed at positions corresponding to their keys, filling missing positions with a default value. var numberObject = { 2: 90, 6: 98 }; console.log("The original object:"); console.log(numberObject); The original object: { '2': 90, '6': ...
Read MoreFetching object keys using recursion in JavaScript
When working with nested objects in JavaScript, you often need to search for specific keys at any level of nesting. Recursion provides an elegant solution for traversing these complex data structures. The Problem Consider a nested object structure where we need to find all values for a specific key across all levels: const people = { Ram: { fullName: 'Ram Kumar', details: { age: ...
Read More