AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 354 of 840

Calculating 1s in binary representation of numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 530 Views

We need to write a JavaScript function that takes an integer num and returns an array where each element represents the count of 1s in the binary representation of numbers from 0 to num. Problem Given a number num, create an array where: Index i contains the count of 1s in binary representation of i Array includes elements for numbers 0 through num (inclusive) For example, if num = 4: Input: 4 Output: [0, 1, 1, 2, 1] Output Explanation Let's break down the binary representations: Number ...

Read More

Constructing an array of first n multiples of an input number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 420 Views

We are required to write a JavaScript function that takes in two numbers, let say m and n. Our function should construct and return an array of first n natural multiples of m. Problem Statement Given two numbers m and n, we need to create a function that returns an array containing the first n multiples of m. For example, if m = 6 and n = 5, the function should return [6, 12, 18, 24, 30]. Method 1: Using a For Loop The most straightforward approach is to use a for loop to iterate ...

Read More

Changing positivity/negativity of Numbers in a list in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 108 Views

We are required to write a JavaScript function that takes in an array of positive as well as negative Numbers and changes the positive numbers to corresponding negative numbers and the negative numbers to corresponding positive numbers in place. Example The code for this will be − const arr = [12, 5, 3, -1, 54, -43, -2, 34, -1, 4, -4]; const changeSign = arr => { arr.forEach((el, ind) => { arr[ind] *= -1; }); }; changeSign(arr); console.log(arr); ...

Read More

How to make a condition for event 'click' inside/outside for multiple divs - JavaScript?

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

In JavaScript, you can detect clicks inside or outside multiple divs using event listeners and the contains() method. This is useful for dropdowns, modals, or interactive UI components. The Problem When working with multiple divs, you need to determine if a click event occurred inside any of the target divs or outside all of them. This requires checking the event target against all div elements. Solution Using Event Delegation Add a single event listener to the document and check if the clicked element is contained within any target div: ...

Read More

Loop through an index of an array to search for a certain letter in JavaScript

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

We need to write a JavaScript function that takes an array of strings and a single character, then returns true if the character exists in any string within the array, false otherwise. Problem Overview The function should search through each string in the array to find if the specified character exists anywhere. This is a common pattern for filtering or validation tasks. Example Implementation Here's how to implement this search functionality: const arr = ['first', 'second', 'third', 'last']; const searchForLetter = (arr = [], letter = '') => { ...

Read More

Encrypting a string using Caesar Cipher in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 791 Views

The Caesar Cipher algorithm is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example: With a left shift of 3, D would be replaced by A, E would become B, and so on. We need to write a JavaScript function that takes in a string to be encrypted as the first argument and a shift amount as the second argument. The shift amount can be a positive or ...

Read More

Joining strings to form palindrome pairs in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 316 Views

We are required to write a JavaScript function that takes in an array of strings as the only argument. The function is supposed to return an array of arrays of all the index pairs joining the strings at which yields a new palindrome string. For example, if the input to the function is: const arr = ['tab', 'cat', 'bat']; Then the output should be: [[0, 2], [2, 0]] Output Explanation Because both the strings 'battab' and 'tabbat' are palindromes when we join: arr[0] + arr[2] ...

Read More

Creating a binary spiral array of specific size in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 246 Views

We are required to write a JavaScript function that takes in a number n. Our function should construct and return an array of N * N order (2-D array), in which the 1s take all the spiralling positions starting from [0, 0] and all the 0s take non-spiralling positions. Therefore, for n = 5, the output will look like: [ [ 1, 1, 1, 1, 1 ], [ 0, 0, 0, 0, 1 ], [ 1, 1, 1, 0, 1 ], [ 1, 0, 0, 0, 1 ], ...

Read More

Merge arrays in column wise to another array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 748 Views

When working with multiple arrays, you often need to combine their elements column-wise to create structured data. This technique merges arrays by their corresponding indices to form an array of objects. Problem Statement Suppose we have three arrays of numbers like these: const code = [123, 456, 789]; const year = [2013, 2014, 2015]; const period = [3, 4, 5]; We need to merge these arrays column-wise to create an array of objects where each object contains the corresponding elements from all three arrays: const output = [ ...

Read More

How to merge two different array of objects using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 595 Views

Suppose, we have two different array of objects that contains information about the questions answered by some people: const arr1 = [ { PersonalID: '11', questionNumber: '1', value: 'Something' }, { PersonalID: '12', questionNumber: '2', value: 'whatever' }, { PersonalID: '13', questionNumber: '3', value: 'anything' }, { PersonalID: '14', questionNumber: '4', value: 'null' } ]; const arr2 = [ { questionNumber: '2', chID: '111', cValue: 'red' }, { questionNumber: '2', chID: '112', cValue: ...

Read More
Showing 3531–3540 of 8,392 articles
« Prev 1 352 353 354 355 356 840 Next »
Advertisements