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 331 of 840
How to add default horizontal scaling to a canvas-type text with JavaScript?
We can add default horizontal scaling to canvas text by using the scale() method on the 2D rendering context. This method transforms all subsequent drawing operations, including text, by scaling them horizontally and vertically. HTML Canvas HTML canvas is a 2D drawing surface that allows developers to create dynamic graphics, charts, and animations using JavaScript. The canvas element provides a powerful API for drawing shapes, text, and images directly in the browser. The canvas element acts as a container for graphics and can be manipulated using the Canvas API to create rich, interactive user experiences without external ...
Read MoreDeleting the last vowel from a string in JavaScript
We are required to write a JavaScript function that takes in a string and returns a new string with the last vowel of each word removed. For example: If the string is − const str = 'This is an example string'; Then the output should be − const output = 'Ths s n exampl strng'; Therefore, let's write the code for this function − Example The code for this will be − const str = 'This is an example string'; const removeLast = word => { ...
Read MoreFinding number plate based on registration number in JavaScript
The car registering system of a city assigns unique identifiers to customers and corresponding number plates to their vehicles. We need to create a JavaScript function that converts a customer ID into its corresponding number plate format. Problem Statement The car registration system assigns two types of identifiers: Customer ID − A natural number between 0 and 17, 558, 423 (inclusive), assigned sequentially starting from 0 Number Plate − Contains a series ...
Read MoreAll right triangles with specified perimeter in JavaScript
We are required to write a JavaScript function that takes in a number that specifies the perimeter for a triangle. Our function should return an array of all the right triangle side triplets whose perimeter matches the specified input. Understanding Right Triangles A right triangle satisfies the Pythagorean theorem: a² + b² = c², where c is the hypotenuse (longest side). For a given perimeter P, we need a + b + c = P. Algorithm Approach We use three nested loops to check all possible combinations of triangle sides. For each triplet, we verify: ...
Read MoreFinding maximum length of common subarray in JavaScript
Finding the maximum length of a common subarray (also known as the longest common subarray) is a classic dynamic programming problem. We need to find the longest contiguous sequence that appears in both arrays in the same order. Problem Statement We are required to write a JavaScript function that takes in two arrays of literals, arr1 and arr2, as the first and the second argument respectively. Our function should return the maximum length of a subarray that appears in both arrays. For example, if the input arrays are: const arr1 = [1, 2, 3, 2, ...
Read MoreDynamic programming to check dynamic behavior of an array in JavaScript
We are required to write a JavaScript function that takes in an array of strings, ordered by ascending length. The function should return true if, for each pair of consecutive strings, the second string can be formed from the first by adding a single letter either at the beginning or end. For example: If the array is given by − const arr = ["c", "ca", "can", "acan", "acane", "dacane"]; Then our function should return true because: "ca" = "c" + "a" (adding "a" at the end) ...
Read MoreFinding sum of all numbers within a range in JavaScript
Problem We are required to write a JavaScript function that takes in an array that specifies a range. Our function should find and return the sum of all the natural numbers falling in the range including the range numbers. Example Following is the code − const range = [4, 67]; const findSum = ([l, h]) => { let sum = 0; for(let i = l; i { // Formula: Sum = n * (first + last) / 2 // where ...
Read MoreReturn the nearest greater integer of the decimal number it is being called on in JavaScript
The Math.ceil() function returns the smallest integer greater than or equal to a given number. This means it "rounds up" to the nearest integer. Syntax Math.ceil(x) Parameters x: A number to round up to the nearest integer. Return Value Returns the smallest integer greater than or equal to the given number. If the input is not a number, returns NaN. Example: Basic Usage console.log(Math.ceil(4.3)); // 5 console.log(Math.ceil(4.8)); // 5 console.log(Math.ceil(5.0)); // 5 (already integer) console.log(Math.ceil(-2.3)); // -2 console.log(Math.ceil(-2.8)); // -2 ...
Read MoreProduct of subarray just less than target in JavaScript
We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument, and a number, target, as the second argument. Our function is supposed to count and return the number of (contiguous) subarrays where the product of all the elements in the subarray is less than target. Problem Example For example, if the input to the function is: const arr = [10, 5, 2, 6]; const target = 100; The expected output is: 8 The 8 subarrays that have product less ...
Read MoreHow does inline JavaScript work with HTML?
In this article, you will understand how inline JavaScript works with HTML. Inline JavaScript represents a code block written in between the tags in an HTML file. The advantage of using inline JavaScript in HTML files is to reduce the round trip of the web browser to the server. What is Inline JavaScript? Inline JavaScript is JavaScript code that is embedded directly within HTML documents using tags. This approach allows you to execute JavaScript without creating separate .js files, making it convenient for small scripts and quick implementations. Example 1: Basic Inline JavaScript Let ...
Read More