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
Javascript Articles
Page 62 of 534
How to create a JSON representation of a Line object using FabricJS?
In this tutorial, we are going to learn about how to create a JSON representation of a Line object using FabricJS. A Line element is one of the basic elements provided in FabricJS. It is used for creating straight lines. Because line elements are geometrically one-dimensional and do not contain an interior, they are never filled. We can create a line object by creating an instance of fabric.Line, specifying the x and y coordinates of the line and adding it to the canvas. In order to create a JSON representation of a Line object, we use the toJSON method. ...
Read MoreCalculate Subtraction of diagonals-summations in a two-dimensional matrix using JavaScript
Suppose we have a square matrix represented by a 2-D array in JavaScript like this: const arr = [ [1, 3, 5], [3, 5, 7], [2, 4, 2] ]; console.log(arr); [ [ 1, 3, 5 ], [ 3, 5, 7 ], [ 2, 4, 2 ] ] We need to write a JavaScript function that calculates the absolute difference between the sum of elements on the two diagonals of the matrix. Understanding the Diagonals In a square matrix, there are two diagonals: ...
Read MoreHow to validate if an element in an array is repeated? - JavaScript
We are required to write a JavaScript function that takes in two arguments: An Array, say arr, of literals that may contain some repeating elements. A number, say limit. The function should validate that no element of the array is repeated more than limit number of times. If any element is repeated more than the limit the function should return false, true otherwise. Using Object to Count Occurrences The most efficient approach is to use an object to count each element's occurrences, then check if any count exceeds ...
Read MoreCompare and fill arrays - JavaScript
We are required to write a function that compares two arrays and creates a third array filling that array with all the elements of the second array and filling null for all those elements that are present in the first array but misses out in the second array. For example − If the two arrays are − const arr1 = ['f', 'g', 'h']; const arr2 = ['f', 'h']; Then the output should be − const output = ['f', null, 'h']; Algorithm Approach The solution uses an offset variable to ...
Read MoreHow to select the middle of an array? - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns the middlemost element(s) of the array. For arrays with odd length, there's one middle element. For arrays with even length, there are two middle elements. For example, if the array is: const arr = [1, 2, 3, 4, 5, 6, 7]; Then the output should be [4] (the middle element at index 3). Using Array Prototype Method We can extend the Array prototype to add a middle() method that returns the middle element(s): ...
Read MoreHow to find the biggest number in an array around undefined elements? - JavaScript
We are required to write a JavaScript function that takes in an array that contains some numbers, some strings and some falsy values. Our function should return the biggest Number from the array. For example − If the input array is the following with some undefined values − const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; Then the output should be 65 Solution Approach We'll filter out non-numeric values and find the maximum among valid numbers. The key is to properly identify numeric values while handling ...
Read MoreBehavior of + operator in JavaScript to store large numbers?
JavaScript's + operator converts strings to numbers, but regular numbers have precision limits. For large integers beyond JavaScript's safe range, use BigInt() to avoid precision loss. The Problem with + Operator for Large Numbers JavaScript numbers use 64-bit floating point, which can safely represent integers up to Number.MAX_SAFE_INTEGER (2^53 - 1). Beyond this limit, the + operator causes precision loss: console.log("JavaScript's safe integer limit:"); console.log(Number.MAX_SAFE_INTEGER); // Small number - works fine var stringValue1 = "100"; console.log("Small number with + operator:"); console.log(+stringValue1); // Large number - precision loss var stringValue2 = "2312123211345545367"; console.log("Large number with ...
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 MoreJavaScript: lexical scoping issue using new keyword constructor while adding inner function?
When using constructor functions with inner functions, a common lexical scoping issue arises where the inner function cannot access the constructor's this context. This happens because inner functions have their own execution context. The Problem Inner functions don't inherit the this binding from their parent constructor, leading to undefined or incorrect values when trying to access constructor properties. function Employee() { this.technologyName = "JavaScript"; function workingTechnology() { // 'this' here refers to global object, not ...
Read MoreHow to new line string - JavaScript?
In JavaScript, there are several ways to create new lines in strings depending on where the output will be displayed. For HTML content, use tags, while for console output or plain text, use the escape sequence . Using Tag for HTML Display When displaying text in HTML elements, use the tag to create line breaks: New Line in HTML Original Text ...
Read More