AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 351 of 840

Converting degree to radian in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 628 Views

Converting degrees to radians is a common mathematical operation in JavaScript, especially when working with trigonometric functions or graphics programming. Understanding Radians The radian is the standard unit for measuring angles in mathematics. One complete circle equals 2π radians, which is equivalent to 360 degrees. This means π radians equals 180 degrees. The Conversion Formula To convert degrees to radians, multiply the degree value by π/180: radians = degrees × (π / 180) Method 1: Using the Standard Formula const degreeToRadian = (degree) => { const ...

Read More

Reversing negative and positive numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Problem We are required to write a JavaScript function that takes in a number and returns its reversed number. One thing that we should keep in mind is that numbers should preserve their sign; i.e., a negative number should still be negative when reversed. Example Following is the code − const num = -224; function reverseNumber(n) { let x = Math.abs(n) let y = 0 while (x > 0) { y = y * 10 + (x ...

Read More

Repeating each character number of times their one based index in a string using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 606 Views

We need to write a JavaScript function that takes a string of lowercase English letters and transforms it according to specific rules: each character should be repeated based on its 1-based index position, with the first letter capitalized, and character groups separated by dashes. Problem Statement Given a string of lowercase English alphabets, construct a new string where: Each character is repeated according to its 1-based index (1st char repeated 1 time, 2nd char repeated 2 times, etc.) The first letter of each repeated group is capitalized Different character groups are separated by dashes ('-') ...

Read More

Negative number digit sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 640 Views

We are required to write a JavaScript function that takes in a negative integer and returns the sum of its digits. Example Input For example: If the number is: -5456 Expected Output Then the output should be: 5+4+5+6 = 20 Using String Manipulation and Reduce The following approach converts the number to string, splits it into digits, and uses reduce to sum them: const num = -5456; const sumNum = num => { return String(num).split("").reduce((acc, val, ind) => { ...

Read More

Remove duplicates and map an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 668 Views

When working with arrays of objects, a common task is extracting unique values from a specific property. This tutorial shows how to remove duplicates and map an array to extract unique "name" values from objects. Problem Statement Suppose we have an array of objects like this: const arr = [ {id: 123, value: "value1", name: "Name1"}, {id: 124, value: "value2", name: "Name1"}, {id: 125, value: "value3", name: "Name2"}, {id: 126, value: "value4", name: "Name2"} ]; Note that some ...

Read More

Finding power set for a set in JavaScript Power Set

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 844 Views

The power set of a set S is the set of all of the subsets of S, including the empty set and S itself. The power set of set S is denoted as P(S). For example If S = {x, y, z}, the subsets are: { {}, {x}, {y}, {z}, {x, y}, {x, z}, {y, z}, {x, y, z} } We need ...

Read More

Reversing consonants only from a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 444 Views

We are required to write a JavaScript function that takes in a string of lowercase English alphabets as the only argument. The function should construct a new string in which the order of consonants is reversed and the vowels hold their relative positions. Problem Statement For example, if the input to the function is: const str = 'somestring'; Then the output should be: const output = 'gomenrtiss'; In this example, consonants 's', 'm', 't', 'r', 'n', 'g' are reversed to 'g', 'n', 'r', 't', 'm', 's', while vowels 'o', ...

Read More

Finding array number that have no matching positive or negative number in the array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 217 Views

We need to write a JavaScript function that finds a number in an array that doesn't have its positive or negative counterpart. For example, in an array containing both 1 and -1, both 2 and -2, but only 3 (without -3), we should return 3. Problem Given an array of integers where each number has its negative or positive complement, except for exactly one number, our function should find and return that unpaired number. Example Input Consider the array [1, -1, 2, -2, 3]. Here, 1 has -1, 2 has -2, but 3 has no -3, ...

Read More

Omitting false values while constructing string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 130 Views

We have an array that contains some string values as well as some false values like null, undefined, 0, and empty strings. We need to write a JavaScript function that takes this array and returns a string constructed by joining values while omitting all falsy values. Understanding Falsy Values In JavaScript, falsy values include: false, 0, "", null, undefined, and NaN. These evaluate to false in boolean contexts. Method 1: Using reduce() with Logical OR The logical OR operator (||) returns the right operand when the left is falsy: const arr = ["Here", ...

Read More

Filter array of objects whose properties contains a value in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 993 Views

Filtering an array of objects based on property values is a common task in JavaScript. This technique allows you to search through all properties of objects to find matches based on a keyword. Sample Data Let's start with an array of objects containing user information: const arr = [{ name: 'Paul', country: 'Canada', }, { name: 'Lea', country: 'Italy', }, { name: 'John', country: 'Italy', }]; The Problem We need to filter objects where any ...

Read More
Showing 3501–3510 of 8,392 articles
« Prev 1 349 350 351 352 353 840 Next »
Advertisements