AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 471 of 840

Creating a JavaScript Object from Single Array and Defining the Key Value?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 198 Views

JavaScript provides several ways to create objects from arrays and define key-value pairs. This is useful when transforming data structures or converting between different formats. Using Object.entries() with map() The most common approach uses Object.entries() to convert an object into an array of key-value pairs, then map() to transform the structure: var studentObject = { 101: "John", 102: "David", 103: "Bob" }; var studentDetails = Object.entries(studentObject).map(([studentId, studentName]) => ({ studentId, studentName })); console.log(studentDetails); ...

Read More

Take two numbers m and n & return two numbers whose sum is n and product m in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 256 Views

We need to write a JavaScript function that takes two numbers m (product) and n (sum) and returns two numbers whose sum equals n and product equals m. If no such numbers exist, the function should return false. This is essentially solving a quadratic equation where we need to find two numbers x and y such that x + y = n and x × y = m. Mathematical Approach We can solve this using the quadratic formula. If x + y = n and x × y = m, then x and y are roots of ...

Read More

From an array of arrays, return an array where each item is the sum of all the items in the corresponding subarray in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 360 Views

Given an array of arrays, each of which contains a set of numbers. We have to write a function that returns an array where each item is the sum of all the items in the corresponding subarray. For example, if we have nested arrays with numbers, we want to calculate the sum of each inner array and return those sums as a new array. Example Input and Expected Output If the input array is: const numbers = [ [1, 2, 3, 4], [5, 6, 7], [8, ...

Read More

How to convert an Image to blob using JavaScript?

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

Converting an image to a blob in JavaScript allows you to work with binary image data for file uploads, downloads, or processing. A blob (Binary Large Object) represents immutable binary data that can be read as text or binary data. What is a Blob? A blob is a file-like object that contains raw binary data. When you convert an image to a blob, you get access to properties like size and MIME type, making it useful for file operations. Using fetch() to Convert Image URL to Blob The most common approach is using the fetch API ...

Read More

Find the longest sub array of consecutive numbers with a while loop in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 404 Views

We are required to write a function with a while-statement that determines the length of the largest consecutive subarray in an array of positive integers. For instance, if we have an array like [6, 7, 8, 6, 12, 1, 2, 3, 4], the longest consecutive sequence is [1, 2, 3, 4] with length 4. Problem Understanding Let's look at some examples to understand the problem better: Input: [6, 7, 8, 6, 12, 1, 2, 3, 4] Consecutive sequence: [1, 2, 3, 4] Output: 4 Input: [5, 6, 1, 8, 9, 7] Consecutive ...

Read More

Assign multiple variables to the same value in JavaScript?

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

JavaScript allows you to assign the same value to multiple variables in a single statement using the assignment operator chaining technique. Syntax var variable1, variable2, variable3; variable1 = variable2 = variable3 = value; You can also declare and assign in one line: var variable1 = variable2 = variable3 = value; Example: Assigning Same Value to Multiple Variables var first, second, third, fourth, fifth; first = second = third = fourth = fifth = 100; console.log("first:", first); console.log("second:", second); console.log("third:", third); console.log("fourth:", fourth); console.log("fifth:", fifth); console.log("Sum of all values:", ...

Read More

How to access JavaScript properties?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 332 Views

In JavaScript, there are three main ways to access object properties. Each method has specific use cases and advantages depending on the situation. Three Methods to Access Properties Dot notation: object.property Square bracket notation: object['property'] Object destructuring: let {property} = object Method 1: Using Dot Notation The dot notation is the most common and readable way to access properties when the property name is known at compile time. ...

Read More

Check if some elements of array are equal JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 433 Views

We have an array of numbers that have got some redundant entries, our job is to write a function that takes in the array and groups all the identical entries into one subarray and returns the new array thus formed. For example − //If the input array is: const arr = [1, 3, 3, 1]; //then the output should be: const output = [[1, 1], [3, 3]]; We will use a HashMap to keep a track of the elements already occurred and iterate over the array using a for loop. Using HashMap Approach ...

Read More

Finding next n leap years in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 638 Views

We are required to write a function that takes a positive integer n and returns an array of next n leap years. We will break this problem into three parts: Part 1: Finding Current Year via JavaScript The code to find current year via JavaScript will be: // getting the current year from a new instance of Date object const year = new Date().getFullYear(); console.log("Current year:", year); Current year: 2024 Part 2: Checking for Leap Year We will now write a function isLeap() that takes in a number and returns ...

Read More

Finding if three points are collinear - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 717 Views

Three or more points that lie on the same straight line are called collinear points. In JavaScript, we can determine if three points are collinear by checking if they have the same slope between each pair of points. Mathematical Concept Three points A, B, and C are collinear if the slope between any two pairs is equal: slope of AB = slope of BC = slope of AC Slope Formula For two points A(x1, y1) and B(x2, y2), the slope is calculated as: Slope = (y2 - y1) / (x2 - ...

Read More
Showing 4701–4710 of 8,392 articles
« Prev 1 469 470 471 472 473 840 Next »
Advertisements