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
How to concatenate the string value length -1 in JavaScript.
In JavaScript, you can concatenate a string value (length-1) times using the Array() constructor with join(). This technique creates an array with empty elements and joins them with your desired string. Syntax new Array(count).join('string') Where count is the number of times you want to repeat the string, and the actual repetitions will be count - 1. How It Works When you create new Array(5), it generates an array with 5 empty slots. The join() method places the specified string between each element, resulting in 4 concatenated strings (length-1). Example var ...
Read MoreCheck if user inputted string is in the array in JavaScript
We need to write a JavaScript program that checks if a user-inputted string exists in a predefined array. The program should return true if the string is found, false otherwise. This is commonly needed when validating user input against a list of acceptable values, such as checking usernames, categories, or allowed options. Using Array.includes() Method The includes() method is the most straightforward way to check if an array contains a specific value. It returns a boolean result. Check ...
Read MoreHow to dynamically combine all provided arrays using JavaScript?
When working with arrays in JavaScript, you might need to generate all possible combinations from multiple arrays. This is commonly known as the Cartesian product of arrays. Suppose we have two arrays of literals like these: const arr1 = ['a', 'b', 'c']; const arr2 = ['d', 'e', 'f']; console.log('Array 1:', arr1); console.log('Array 2:', arr2); Array 1: [ 'a', 'b', 'c' ] Array 2: [ 'd', 'e', 'f' ] We need a JavaScript function that takes multiple arrays and builds all possible combinations from them. For these two arrays, we want to get ...
Read MoreSquare matrix rotation in JavaScript
We are required to write a JavaScript function that takes in an array of arrays of n * n order (square matrix). The function should rotate the array by 90 degrees (clockwise). The condition is that we have to do this in place (without allocating any extra array). For example − If the input array is − const arr = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; Then the rotated array should look like − const output = [ ...
Read MoreWeight sum of a nested array in JavaScript
We are required to write a JavaScript function that takes in a nested array, arr (nested up to any level) as the only argument. The function should calculate the weighted sum of the nested array and return that sum. For calculating weighted sum, we multiply each element with its level of nesting and add throughout the array. Elements at the first level are multiplied by 1, second level by 2, and so on. Problem Example For example, if the input to the function is: const arr = [4, 7, [6, 1, [5, 2]]]; ...
Read MoreReversing all alphabetic characters in JavaScript
We are required to write a JavaScript function that takes in a string and reverses only the alphabetic characters, omitting all non-alphabetic characters from the result. Problem Given a string containing both alphabetic and non-alphabetic characters, we need to create a function that filters out non-alphabetic characters and returns the alphabetic characters in reverse order. Example Following is the code − const str = 'exa13mple'; function reverseLetter(str) { const res = str.split('') .reverse() .filter(val ...
Read MoreFinding the character with longest consecutive repetitions in a string and its length using JavaScript
We need to write a JavaScript function that finds the character with the longest consecutive repetitions in a string and returns both the character and its count as an array. Problem Given a string, we want to identify which character appears the most times consecutively and return an array containing the character and its consecutive count. Example Here's a solution that tracks consecutive characters and finds the maximum: const str = 'tdfdffddffsdsfffffsdsdsddddd'; const findConsecutiveCount = (str = '') => { if (!str) return ['', 0]; ...
Read Morecrypto.randomFillSync() Method in Node.js
The crypto.randomFillSync() method takes a buffer argument and returns the buffer by filling it with its encrypted value. As the name suggests, this will be a sync process. Syntax crypto.randomFillSync(buffer, [offset], [size]) Parameters The above parameters are described as below − buffer – This field contains the data content. Possible buffer types are: string, TypedArray, Buffer, ArrayBuffer, DataView. The size of the buffer cannot be greater than 2**31-1. ...
Read MoreHow to get Property Descriptors of an Object in JavaScript?
In JavaScript, property descriptors define the behavior and characteristics of object properties. The Object.getOwnPropertyDescriptor() method returns a descriptor object that describes a specific property of an object, including its value and configuration attributes. Syntax Object.getOwnPropertyDescriptor(obj, prop) Parameters obj − The object whose property descriptor you want to retrieve. prop − The name or Symbol of the property whose descriptor you want to retrieve. This method returns a property descriptor object if the property exists, or undefined if it doesn't. Property Descriptor Attributes A ...
Read MoreHow to create a Rectangle with background colour using FabricJS?
In this tutorial, we are going to create a Rectangle with background colour using FabricJS. Rectangle is one of the various shapes provided by FabricJS. In order to create a rectangle, we will have to create an instance of fabric.Rect class and add it to the canvas. The backgroundColor property allows us to assign a colour to our object's background. It is the colour of the container where the rectangle lives and is rectangular in shape for the rectangle. Syntax new fabric.Rect({ backgroundColor: String }: Object) Parameters ...
Read More