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
Moving every alphabet forward by 10 places in JavaScript
Problem We need to write a JavaScript function that takes a string of English alphabets and shifts every letter forward by 10 positions. When a letter goes past 'z', it wraps around to start again at 'a'. Example Following is the code − const str = 'sample string'; const moveStrBy = (num = 10) => { return str => { const calcStr = (ch, code) => String .fromCharCode(code + (ch.charCodeAt(0) - ...
Read MoreJavaScript Strings: Replacing i with 1 and o with 0
We are required to write a function that takes in a string as one and only argument and returns another string that has all 'i' and 'o' replaced with '1' and '0' respectively. It's one of those classic for loop problems where we iterate over the string with its index and construct a new string as we move through. Using For Loop The most straightforward approach is to iterate through each character and build a new string: const string = 'Hello, is it raining in Amsterdam?'; const replaceChars = (str) => { ...
Read MoreInserting empty string in place of repeating values in JavaScript
We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end. For example: If we find 4 duplicate values we have to remove them all and insert four empty strings at the end. Therefore, let's write the code for this function − Example The code for this will be − const arr = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 5, 12, 1, 23, 4, 1]; const deleteAndInsert = arr => { ...
Read MoreFinding square root of a non-negative number without using Math.sqrt() JavaScript
We are required to write a JavaScript function that takes in a non-negative integer and computes and returns its square root without using Math.sqrt(). We can floor off a floating-point number to an integer. For example: For the number 15, we need not return the precise value, we can just return the nearest smaller integer value that will be 3, in case of 15. We will make use of the binary search algorithm to converge to the square root of the given number. Binary Search Approach The binary search method works by maintaining a range [left, ...
Read MoreCombine array of objects in JavaScript
Suppose, we have an array of objects that contains data about some students like this: const arr = [{ name: 'A', idNo: 1, marks: { math: 98, sci: 97, eng: 89 } }, { name: 'B', idNo: 2, marks: { ...
Read MoreFinding sequential digit numbers within a range in JavaScript
A number has sequential digits if and only if each digit in the number is one more than the previous digit. For example, 1234, 2345, and 6789 are sequential digit numbers, while 1324 and 2468 are not. Problem Statement We need to write a JavaScript function that takes an array of two elements specifying a range and returns a sorted array of all integers within that range (inclusive) that have sequential digits. For example, if the input is: const arr = [1000, 13000]; The output should be: [1234, 2345, 3456, 4567, ...
Read MoreSum of perimeter of all the squares in a rectangle using JavaScript
Problem: Suppose there are 5 squares embedded inside a rectangle like this − 1 1 2 3 5 Side lengths: 1, 1, 2, 3, 5 Perimeters: 4, 4, 8, 12, 20 The squares follow a Fibonacci-like pattern ...
Read MoreHow to set the border scale factor of Rectangle using FabricJS?
In this tutorial, we are going to learn how to set the border scale factor of a Rectangle 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. We can use the borderScaleFactor property which specifies the scale factor of the object's controlling borders. This property controls the thickness of the selection border that appears when an object is selected on the canvas. Syntax ...
Read MoreNatural Sort in JavaScript
Natural sort in JavaScript refers to sorting arrays containing mixed data types (numbers and strings) in a way that numbers come first in ascending order, followed by strings in alphabetical order. Problem Statement When sorting mixed arrays with the default sort() method, JavaScript converts everything to strings, leading to incorrect ordering. We need a custom sorting function that handles numbers and strings separately. Example Input and Expected Output Let's say this is our array: const arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9]; console.log("Original array:", arr); Original array: ...
Read MoreCumulative average of pair of elements in JavaScript
We have an array of numbers and need to write a function that returns an array with the average of each element and its predecessor. For the first element, since there's no predecessor, we return the element itself. Let's implement this using the Array.prototype.map() method to transform each element based on its position. How It Works The algorithm works as follows: For the first element (index 0): return the element itself For other elements: calculate (current + previous) / 2 Example const arr = [3, 5, 7, 8, 3, 5, 7, ...
Read More