AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 303 of 840

Validating a boggle word using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 576 Views

A Boggle board is a 2D array of individual characters. We need to validate whether a given word can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without reusing any previously used cells. Problem Given a Boggle board like this: const board = [ ["I", "L", "A", "W"], ["B", "N", "G", "E"], ["I", "U", "A", "O"], ["A", "S", "R", "L"] ]; We need to check if words like "BINGO" or "LINGO" are valid. Valid words are ...

Read More

Adding binary without converting in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 345 Views

Adding binary numbers without converting to decimal requires simulating manual binary addition with carry handling. This approach processes bits from right to left, just like traditional addition. Problem Statement We need to write a JavaScript function that takes two binary strings and returns their sum as a binary string, without converting to decimal numbers. Input: const str1 = '1101'; const str2 = '10111'; Expected Output: '100100' Algorithm Overview The solution works by: Reversing both strings to process from least significant bit Adding corresponding bits with carry propagation Building the ...

Read More

Code to construct an object from a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 344 Views

We are required to write a function that takes in a string as the first and the only argument and constructs an object with its keys based on the unique characters of the string and value of each key being defaulted to 0. For example: If the input string is − const str = 'hello world!'; Output Then the output object should be − const obj = { "h": 0, "e": 0, "l": 0, "o": 0, " ": 0, "w": 0, "r": 0, "d": 0, "!": 0 }; Using Array.reduce() ...

Read More

Summing up digits and finding nearest prime in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 161 Views

We are required to write a JavaScript function that takes in a number, finds the sum of its digits and returns a prime number that is just greater than or equal to the sum. This problem involves two main steps: calculating the digit sum and finding the nearest prime number greater than or equal to that sum. Breaking Down the Solution Our solution requires three functions: digitSum() - calculates the sum of all digits in a number isPrime() - checks if a number is prime nearestPrime() - finds the nearest prime >= digit sum ...

Read More

Sum of array object property values in new array of objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 767 Views

When working with arrays of objects, we often need to group objects by a common property and sum up their numeric values. This is particularly useful when dealing with student records, sales data, or any scenario where duplicate categories need to be consolidated. Suppose we have an array of objects that contains data about students and their marks: const arr = [ { subject: 'Maths', marks: '40', noOfStudents: '5' }, { subject: 'Science', marks: '50', noOfStudents: '16' }, { subject: 'History', marks: '35', noOfStudents: '23' }, { subject: 'Science', ...

Read More

Making array unique in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 291 Views

In JavaScript, making array elements unique by incrementing duplicates is a common problem. This approach finds the minimum number of incremental moves needed to ensure all array elements are unique. Problem Statement Given an array of numbers, we need to find the minimum number of moves to make all elements unique. A move consists of incrementing any element by 1. For example, with the array [12, 15, 7, 15], we need 1 move to make it unique by changing one 15 to 16. Algorithm Approach The strategy is to sort the array first, then iterate ...

Read More

Taking common elements from many arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 560 Views

We are required to write a JavaScript function that takes in any arbitrary number of arrays and returns an array of elements that are common to all arrays. If there are no common elements, then we should return an empty array. This problem requires finding the intersection of multiple arrays, which means identifying elements that exist in every provided array. Understanding the Problem The intersection of arrays means finding elements that appear in all arrays. For example, if we have arrays [1, 2, 3], [2, 3, 4], and [3, 4, 5], the common element is 3. ...

Read More

How to round up to the nearest N in JavaScript

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

In JavaScript, rounding a number to the nearest multiple of N requires dividing by N, rounding the result, then multiplying back by N. Consider this example: const num = 76; When rounding to different factors: Round to nearest 10: 76 becomes 80 Round to nearest 100: 76 becomes 100 Round to nearest 1000: 76 becomes 0 We need a JavaScript function that takes a number and a rounding factor, returning the rounded result. Syntax const roundOffTo = (num, factor = 1) => { const ...

Read More

Checking for centrally peaked arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 157 Views

We are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument. Our function should check whether the input array is a centrally peaked array or not. If it is a centrally peaked array, we should return true, false otherwise. Conditions for Centrally Peaked Array The conditions for being a centrally peaked array are: arr.length >= 3 There exists some i with 0 < i < arr.length - 1 such that: ...

Read More

Checking if a string contains all unique characters using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 476 Views

Problem We are required to write a JavaScript function that takes in a string and returns true if all the characters in the string appear only once and false otherwise. Method 1: Using indexOf() and lastIndexOf() This approach compares the first and last occurrence of each character. If they differ, the character appears multiple times. const str = 'thisconaluqe'; const allUnique = (str = '') => { for(let i = 0; i < str.length; i++){ const el = str[i]; ...

Read More
Showing 3021–3030 of 8,392 articles
« Prev 1 301 302 303 304 305 840 Next »
Advertisements