Find what numbers were pressed to get the word (opposite of phone number digit problem) in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

223 Views

The mapping of the numerals to alphabets in the old keypad type phones used to be like this: const mapping = { 1: [], 2: ['a', 'b', 'c'], 3: ['d', 'e', 'f'], 4: ['g', 'h', 'i'], 5: ['j', 'k', 'l'], 6: ['m', 'n', 'o'], 7: ['p', 'q', 'r', 's'], 8: ['t', 'u', 'v'], 9: ['w', 'x', 'y', 'z'] }; console.log(mapping); { '1': [], '2': [ 'a', 'b', 'c' ], '3': ... Read More

Counting the number of 1s upto n in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

334 Views

Counting the number of 1s from 1 to n is a common algorithmic problem in JavaScript. We need to count how many times the digit "1" appears in all positive integers up to and including n. For example, if n = 31, the digit "1" appears in: 1, 10, 11 (twice), 12, 13, 14, 15, 16, 17, 18, 19, 21, 31. That's a total of 14 occurrences. Problem Analysis The challenge is to efficiently count digit occurrences without iterating through every number. We can solve this using digit-by-digit analysis or a simpler brute force approach. Method ... Read More

Finding distance between two points in a 2-D plane using JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

476 Views

Problem We are required to write a JavaScript function that takes in two objects both having x and y property specifying two points in a plane. Our function should find and return the distance between those two points using the Euclidean distance formula. Distance Formula The distance between two points (x₁, y₁) and (x₂, y₂) is calculated using: distance = √[(x₂ - x₁)² + (y₂ - y₁)²] Example Following is the code: const a = {x: 5, y: -4}; const b = {x: 8, y: 12}; const distanceBetweenPoints ... Read More

Reshaping 2-D array in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

1K+ Views

In JavaScript, reshaping a 2-D array means converting it to a new matrix with different dimensions while preserving the original element order. This is useful for data manipulation and matrix operations. Problem Statement We need to write a JavaScript function that takes a 2-D array and reshapes it into a new matrix with specified rows and columns. The elements should maintain their row-traversing order from the original array. For example, if we have: const arr = [ [6, 7], [8, 9] ]; const r = 1, c ... Read More

What is difference between forEach() and map() method in JavaScript?

Imran Alam
Updated on 15-Mar-2026 23:19:00

42K+ Views

JavaScript provides several ways to loop through arrays. Two of the most commonly used methods are forEach() and map(). While both iterate through array elements, they serve different purposes and have distinct characteristics. The forEach() Method The forEach() method executes a callback function for each array element. It's designed for performing side effects like logging, updating DOM elements, or modifying external variables. Importantly, forEach() always returns undefined. Syntax array.forEach(function(element, index, array) { // Execute code for each element }); forEach() Example ... Read More

FabricJS – How to create the instance of fabric.Image from its object representation?

Rahul Gurung
Updated on 15-Mar-2026 23:19:00

2K+ Views

In this tutorial, we are going to show how you can create the instance of fabric.Image from its object representation using FabricJS. We can create an Image object by creating an instance of fabric.Image. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. In order to create the instance of fabric.Image from its object representation, we use the fromObject method. Syntax fabric.Image.fromObject(object, callback) Parameters object − This parameter accepts ... Read More

Object comparison Complexity in JavaScript using comparison operator or JSON.stringlify()?

AmitDiwan
Updated on 15-Mar-2026 23:19:00

224 Views

In JavaScript, comparing objects using comparison operators (== or ===) checks reference equality, not content equality. For content comparison, JSON.stringify() can be used with limitations. The Problem with Comparison Operators When comparing objects with == or ===, JavaScript checks if both variables reference the same object in memory, not if their contents are identical: var object1 = { firstName: "David" }; var object2 = { firstName: "David" }; var object3 = object1; console.log("object1 == object2:", object1 == object2); // false - different objects console.log("object1 === object2:", object1 === object2); // false - different ... Read More

Replace multiple instances of text surrounded by specific characters in JavaScript?

AmitDiwan
Updated on 15-Mar-2026 23:19:00

381 Views

Let's say we have a string where certain text is surrounded by special characters like hash (#). We need to replace these placeholders with actual values. var values = "My Name is #yourName# and I got #marks# in JavaScript subject"; We need to replace the special character placeholders with valid values. For this, we use replace() along with shift(). Using replace() with shift() The replace() method with a regular expression can find all instances of text surrounded by hash characters. The shift() method removes and returns the first element from an array, making it ... Read More

JavaScript Sum of two objects with same properties

Disha Verma
Updated on 15-Mar-2026 23:19:00

2K+ Views

In JavaScript, summing two objects with identical properties is a common task when working with data aggregation. This involves creating a new object where each property's value is the sum of corresponding values from the input objects. In JavaScript, objects are data structures that store key-value pairs. When you have multiple objects with the same property names, you often need to combine their values mathematically. Understanding the Problem When you have two objects with the same property names, the goal is to create a new object where each property's value is the sum of the matching values ... Read More

Are the strings anagrams in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

245 Views

Two strings are said to be anagrams of each other if by rearranging, rephrasing or shuffling the letters of one string we can form the other string. Both strings must contain exactly the same characters with the same frequency. For example, 'something' and 'emosghtin' are anagrams of each other because they contain the same letters with the same count. We need to write a JavaScript function that takes two strings and returns true if they are anagrams of each other, false otherwise. Method 1: Character Frequency Count This approach counts the frequency of each character in ... Read More

Advertisements