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
Finding a number, when multiplied with input number yields input number in JavaScript
We need to write a JavaScript function that takes a positive integer n and a positive integer p, then finds a value k such that the sum of digits raised to successive powers equals k * n. Problem Statement Given a number n with digits a, b, c, d... and a power p, we want to find integer k where: (a^p + b^(p+1) + c^(p+2) + d^(p+3) + ...) = n * k If such k exists, return k; otherwise return -1. Example Walkthrough For number 695 and p = 2: 6^2 + ...
Read MoreUsing Kadane’s algorithm to find maximum sum of subarray in JavaScript
Kadane's algorithm is a powerful dynamic programming technique used to find the maximum sum of a contiguous subarray within an array. This algorithm is particularly useful for solving problems involving sequences of numbers where you need to find the optimal contiguous subsequence. Problem Statement Given an array of integers, find the contiguous subarray with the maximum sum and return that sum. Sample Input: arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4] Sample Output: Maximum sum: 6 (from subarray [4, -1, 2, 1]) Method 1: Naive Approach The ...
Read MoreHow to create an Ellipse with a background color using FabricJS?
In this tutorial, we are going to create an Ellipse with background color using FabricJS. Ellipse is one of the various shapes provided by FabricJS. In order to create an ellipse, we have to create an instance of fabric.Ellipse class and add it to the canvas. The backgroundColor property allows us to assign a color to our object's background. It is the color of the container where the ellipse lives and is rectangular in shape for ellipse. Syntax new fabric.Ellipse({ backgroundColor: String }: Object) Parameters ...
Read MoreHow to change the font style of a Textbox using FabricJS?
In this tutorial, we are going to see how to change the font style of a Textbox using FabricJS. We can customize, stretch or move around the text written in a textbox. In order to create a textbox, we will have to create an instance of fabric.Textbox class and add it to the canvas. Font style refers to the style of characters typed within a textbox. Syntax new fabric.Textbox(text: String, { fontStyle: String }: Object) Parameters text − This parameter accepts a String ...
Read MoreHow to Create Pong Game in JavaScript?
A Pong game is a two-player paddle game where each player's task is to save the ball from hitting the wall. Whenever player 1 hits the ball at the opponent's wall then player one will receive a point, and similarly, player 2 will get a point if he/she hits the ball at the opponent's wall. In this tutorial, we will create a Pong Game in JavaScript using HTML5 Canvas. Game Controls Before implementing the game, let's understand the controls: Left Player: Use 'Z' key to move up and 'S' key to move down ...
Read MoreHow to validate email address using RegExp in JavaScript?
Anyone can make a mistake while entering the email in the input field. So, it's the developer's responsibility to check if users have entered a valid email string. Many libraries are available to validate email addresses, which we can use with various JavaScript frameworks, but not with vanilla JavaScript. However, if we want to use any library, we need to use its CDN. Here, we will use the regular expression to validate the email address in vanilla JavaScript. Basic Email Validation Pattern We have used the below regular expression pattern in our first example to validate the email. ...
Read MoreDynamic Programming: return all matched data in JavaScript
Dynamic programming in JavaScript can be used to efficiently search and retrieve data from nested structures. This article demonstrates how to search for all matching cities in a complex JSON object containing country and province information. The Data Structure We have a nested JSON object with countries, provinces, and cities: const countryInfo = { country: [{ name: "Bangladesh", province: [{ name:"Dhaka", ...
Read MoreFinding upper elements in array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return an array of all the elements from the input array that are greater than or equal to the number taken as the second argument. Therefore, let's write the code for this function — Using for Loop The most straightforward approach uses a for loop to iterate through the array and check each element: const arr = [56, 34, 2, 7, 76, 4, 45, 3, ...
Read MoreFinding all possible combined (plus and minus) sums of n arguments JavaScript
We need to write a JavaScript function that takes any number of arguments (all numbers) and finds the sum closest to zero from all possible combinations of addition and subtraction. For example, with arguments 1, 2, 3, the possible combinations are: 1 + 2 + 3 = 6 1 - 2 - 3 = -4 1 + 2 - 3 = 0 1 - 2 + 3 = 2 The function should return the sum closest to 0, which in this case is 0. Algorithm Approach We use a Set to store all ...
Read MoreCounting number of 9s encountered while counting up to n in JavaScript
We need to write a JavaScript function that counts how many times the digit "9" appears when counting from 0 to a given number n. For example, counting from 0 to 100 includes numbers like 9, 19, 29, 90, 91, 99, etc., where "9" appears multiple times. Problem We are required to write a JavaScript function that takes in a number n. Our function should count and return the number of times we will have to use the digit 9 while counting from 0 to n. Example Following is the code − const num ...
Read More