AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 357 of 840

I'm trying to make an id searcher which does a thing when you input the right id. However, the JavaScript if statement always runs. How?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 119 Views

In JavaScript, using the assignment operator (=) instead of comparison operators (== or ===) in an if statement causes the condition to always evaluate as true. This is because assignment returns the assigned value, which is typically truthy. The Problem: Assignment vs Comparison When you accidentally use = in a condition, you're assigning a value instead of comparing: let searchId = 10001; let currentId = 10002; // Wrong: This assigns searchId to currentId and always runs if (currentId = searchId) { console.log("This always runs! currentId is now:", currentId); } // ...

Read More

Implement Bubble sort with negative and positive numbers – JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 984 Views

Bubble sort is a simple sorting algorithm that works by repeatedly comparing adjacent elements and swapping them if they're in the wrong order. It works equally well with arrays containing both negative and positive numbers. Let's say the following is our unsorted array with negative and positive numbers: var arr = [10, -22, 54, 3, 4, 45, 6]; How Bubble Sort Works Bubble sort compares each pair of adjacent elements and swaps them if the first element is greater than the second. This process continues until no more swaps are needed, meaning the array ...

Read More

How to merge two object arrays of different size by key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 876 Views

When working with object arrays of different sizes, we often need to merge them based on a common key. This is useful when combining data from different sources that share a common identifier. Suppose we have an object like this: const obj = { "part1": [{"id": 1, "a": 50}, {"id": 2, "a": 55}, {"id": 4, "a": 100}], "part2":[{"id": 1, "b": 40}, {"id": 3, "b": 45}, {"id": 4, "b": 110}] }; We need to merge part1 and part2 arrays to form a single array where objects with the same id ...

Read More

Finding square root of a number without using Math.sqrt() in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 864 Views

We are required to write a JavaScript function that takes in a positive integer as the only argument. The function should find and return the square root of the number provided as the input. Newton's Method (Recommended) The most efficient approach is Newton's method (also called the Babylonian method), which uses iterative approximation to converge on the square root. const squareRoot = (num, precision = 0) => { if (num deviation) { res -= ((res ** 2) - num) / (2 * res); ...

Read More

Moving all zeroes present in the array to the end in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 606 Views

We are required to write a JavaScript function that takes in an array of literals that might contain some 0s. Our function should tweak the array such that all the zeroes are pushed to the end and all non-zero elements hold their relative positions. Problem Given an array containing zeros and non-zero elements, we need to move all zeros to the end while maintaining the relative order of non-zero elements. Method 1: Using Two-Pass Approach This approach first collects non-zero elements, then adds zeros at the end: const arr = [5, 0, 1, 0, ...

Read More

Greatest number divisible by n within a bound in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 419 Views

Problem We need to write a JavaScript function that takes in a divisor number n and a bound number b. Our function should find the largest integer num, such that: num is divisible by n (the divisor) num is less than or equal to b (the bound) num is greater than 0 Brute Force Approach The straightforward approach is to iterate through all multiples of n and find the largest one within the bound: const n = 14; const b = 400; const biggestDivisible = ...

Read More

Reduce array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 251 Views

In JavaScript, arrays can be transformed using various methods. This article demonstrates how to extract and convert time values from an array of objects into a more usable format. Suppose we have an array of objects containing time strings: const arr = [ {"time":"18:00:00"}, {"time":"10:00:00"}, {"time":"16:30:00"} ]; We need to create a function that: Extracts the time strings from each object Converts times like "18:00:00" to arrays like [18, 0] Returns an array containing all converted time arrays Using map() to Transform ...

Read More

Finding all possible ways of integer partitioning in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 825 Views

The partition of a positive integer n is a way of writing n as a sum of positive integers. Two sums that differ only in the order of their summands are considered the same partition. For example, 4 can be partitioned in five distinct ways: 4 3 + 1 2 + 2 2 + 1 + 1 1 + 1 + 1 + 1 We are required to write a JavaScript function that takes in a positive integer as the only argument. The function should find and return all the possible ways of partitioning that ...

Read More

All ways of balancing n parenthesis in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 208 Views

Problem We are required to write a JavaScript function that takes in a number n. Our function should return an array showing all the ways of balancing n parenthesis. For example, for n = 3, the output will be: ["()()()", "(())()", "()(())", "(()())", "((()))"] Approach We use a recursive backtracking approach where we keep track of how many opening and closing parentheses we can still use. At each step, we can add an opening parenthesis if we have any left, or a closing parenthesis if it would create a valid balance. Example ...

Read More

Finding the sum of all common elements within arrays using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 348 Views

Problem We are required to write a JavaScript function that takes in three arrays of numbers. Our function should return the sum of all those numbers that are common in all three arrays. Example Following is the code − const arr1 = [4, 4, 5, 8, 3]; const arr2 = [7, 3, 7, 4, 1]; const arr3 = [11, 0, 7, 3, 4]; const sumCommon = (arr1 = [], arr2 = [], arr3 = []) => { let sum = 0; for(let i = 0; i ...

Read More
Showing 3561–3570 of 8,392 articles
« Prev 1 355 356 357 358 359 840 Next »
Advertisements