AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 335 of 840

Squaring every digit of a number using split() in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 270 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should then square every digit of the number, append them and yield the new number. For example − If the input number is − const num = 12349; Then the output should be − const output = 1491681; because '1' + '4' + '9' + '16' + '81' = 1491681 How It Works The solution uses split('') to convert each digit into an array element, then ...

Read More

Returning the expanded form of a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 426 Views

The expanded form of a number breaks down each digit into its place value. For example, 1234 becomes "1000 + 200 + 30 + 4". This is useful for teaching place value concepts and number decomposition. Problem We need to write a JavaScript function that takes a number and returns a string showing the expanded form, indicating the place value of each non-zero digit. Example const num = 56577; const expandedForm = (num = 0) => { const str = String(num); let res = ''; ...

Read More

Checking if decimals share at least two common 1 bits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 165 Views

We need to write a JavaScript function that takes two numbers and returns true if they have at least two common 1 bits in their binary representations at the same positions. Problem Given two decimal numbers, we want to check if their binary representations share at least two 1 bits at the same index positions. For example, if we have numbers 10 (binary: 1010) and 15 (binary: 1111), we need to compare bit by bit and count matching 1s. Algorithm Approach The solution involves converting both numbers to binary strings, aligning them by removing extra leading ...

Read More

Forming the nearest time using current time in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 486 Views

In JavaScript, finding the nearest time using current time digits is a common algorithmic problem. We need to form the next closest time by reusing only the available digits from the input time. Problem Statement We are required to write a JavaScript function that takes in a string representing time in "HH:MM" format. The function should form the next closest time by reusing only the current digits, with no limit on how many times a digit can be reused. Example Input and Output For the input time '19:34': Input: '19:34' Available digits: 1, 9, ...

Read More

Grouping of same kind of numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 215 Views

We have an array of numbers with both negative and non-negative values. Our goal is to count consecutive groups of non-negative numbers (positives and zeros) in the array. const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; console.log("Array:", arr); Array: [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0] In this array, we need to identify consecutive groups of non-negative numbers. Looking at the array: Index 3: single element 0 forms one group Indices 9-11: elements 0, 1, 0 form another group ...

Read More

Filtering array of objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 856 Views

Filtering arrays of objects is a common task in JavaScript. This article shows how to filter an array of objects based on values from another array using the filter() and includes() methods. Problem Statement Suppose we have two arrays - one containing literal values and another containing objects: const source = [1, 2, 3, 4, 5]; const cities = [{ city: 4 }, { city: 6 }, { city: 8 }]; console.log("Source array:", source); console.log("Cities array:", cities); Source array: [1, 2, 3, 4, 5] Cities array: [{ city: 4 }, { city: ...

Read More

Calculating time taken to type words in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 354 Views

In JavaScript, we can calculate the time taken to type words on a custom keyboard where keys are arranged alphabetically (a-z) instead of the traditional QWERTY layout. Problem Setup We make two key assumptions: The fingertip starts at index 0 (key 'a') Time to move between keys equals the absolute difference of their positions. For example, moving from 'a' (index 0) to 'k' (index 10) takes |0 - 10| = 10 time units Example Walkthrough For the string 'dab', the movements are: 'a' ...

Read More

Currified function that multiples array elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 264 Views

Problem We need to write a JavaScript function that takes an array and returns another function. This returned function takes a number and produces a new array where each element is the product of the corresponding element from the original array and the number. What is a Curried Function? A curried function breaks down a function that takes multiple arguments into a series of functions that each take a single argument. In this case, instead of multiply(array, number), we have multiply(array)(number). Example Following is the code − const arr = [2, 5, 2, 7, 8, 4]; ...

Read More

Finding the first non-consecutive number in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 461 Views

In JavaScript, finding the first non-consecutive number in an array means identifying the first element that is not exactly one more than its previous element. This is useful for detecting gaps in sequential data. Problem Statement We need to write a JavaScript function that takes an array of numbers and returns the first element that breaks the consecutive sequence. The function should return the element that is not the natural successor (+1) of its previous element. Algorithm The approach is to iterate through the array and compare each element with its previous element. When we find ...

Read More

Forming palindrome using at most one deletion in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 245 Views

We need to write a JavaScript function that checks if a string can be made into a palindrome by deleting at most one character. Problem Given a string, determine if it can become a palindrome after removing at most one character. A palindrome reads the same forwards and backwards. For example, with the input string 'dr.awkward', we can remove the '.' character to get 'drawkward', which is a palindrome. Algorithm Approach We use a two-pointer technique: Compare characters from both ends moving inward When we find a mismatch, try removing either the left or ...

Read More
Showing 3341–3350 of 8,392 articles
« Prev 1 333 334 335 336 337 840 Next »
Advertisements