AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 450 of 840

Joining two strings with two words at a time - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 220 Views

We are required to write a JavaScript function that takes in two strings, creates and returns a new string with first two characters of first string, next two characters of second string, then first, then second and so on. For example: If the strings are: const str1 = 'Hello world'; const str2 = 'How are you btw'; Then the output should be: 'HeHollw o arwoe rlyodu btw' Approach The algorithm alternates between the two strings, taking two characters at a time. When one string is exhausted, it appends the ...

Read More

Sum all similar elements in one array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 477 Views

We are required to write a JavaScript function that takes in an array of numbers and sums all the identical numbers together at one index, removing duplicates while preserving the first occurrence position. For example, if we have repeated numbers in an array, we want to combine them into a single sum at the position where they first appear. Problem Statement If the input array is: const arr = [20, 10, 15, 20, 15, 10]; Then the output should be: const output = [40, 20, 30]; Here, 20 appears ...

Read More

Looping over matches with JavaScript regular expressions

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 958 Views

In JavaScript, you can loop over regular expression matches using several methods. The most common approaches are using match() with the global flag, matchAll(), or exec() in a loop. Using match() with Global Flag The match() method with the global flag (g) returns an array of all matches, which you can then iterate over: Regex Matches Loop body { ...

Read More

How to assign values to an array with null/empty objects in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 974 Views

In JavaScript, you can assign values to an array containing null or empty objects using various methods. This is useful when you need to populate placeholder objects with actual data. Method 1: Using forEach() with Object.keys() This approach iterates through an array of empty objects and assigns properties from a source object: Assign Values to Empty Objects Assign values to an array with null/empty objects Assign Values ...

Read More

Find the shortest string in an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 797 Views

We are required to write a JavaScript function that takes in an array of strings and returns the shortest string in the array. This is a common programming problem that can be solved efficiently using different approaches. We will explore multiple methods to find the shortest string, from simple loops to functional programming approaches. Using for Loop The most straightforward approach uses a for loop to iterate through the array and track the shortest string: const arr = ['this', 'can', 'be', 'some', 'random', 'sentence']; function findShortestString(strings) { if (strings.length === ...

Read More

Can we convert two arrays into one JavaScript object?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 255 Views

In JavaScript, you can convert two arrays into a single object by using one array as keys and another as values. This is useful when you have related data stored in separate arrays. Using forEach() Method The most common approach is to use forEach() to iterate through the keys array and map each key to its corresponding value: Convert Arrays to Object Convert Two Arrays into One JavaScript Object ...

Read More

Convert buffer to readable string in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 792 Views

In Node.js, buffers store binary data and need to be converted to strings for readability. The toString() method with encoding parameters handles this conversion. What is a Buffer? A Buffer is a Node.js object that represents a fixed-size chunk of memory allocated outside the JavaScript heap. It's used to handle binary data directly. Basic Buffer to String Conversion Use the toString() method with the appropriate encoding. UTF-8 is the most common encoding for text data. // Create a buffer from string var actualBufferObject = Buffer.from('[John Smith]', 'utf8'); console.log("The actual buffer object:"); console.log(JSON.stringify(actualBufferObject)); // ...

Read More

Retrieve user id from array of object - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 976 Views

Suppose, we have an array of objects where the user names are mapped to some unique ids like this − const arr = [ {"4": "Rahul"}, {"7": "Vikram"}, {"6": "Rahul"}, {"3": "Aakash"}, {"5": "Vikram"} ]; As apparent in the array, same names can have more than one ids but same ids cannot be used to map two different names. We are required to write a JavaScript function that takes in one such array as the first argument and a name ...

Read More

JavaScript Cursor property

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 236 Views

The JavaScript cursor property sets or returns the cursor type that will be displayed when hovering over an element. This property allows you to change the mouse cursor appearance dynamically using JavaScript. Syntax element.style.cursor = "cursorType"; Example: Changing Cursor on Button Click body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample { ...

Read More

Reversing the prime length words - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 241 Views

We are required to write a JavaScript function that takes in a string that contains words joined by whitespaces. Our function should create a new string that has all the words from the original string, but words whose length is a prime number should be reversed (i.e., words with length 2, 3, 5, 7, 11, etc.). What are Prime Numbers? A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example: 2, 3, 5, 7, 11, 13, etc. Algorithm Our approach involves three main steps: ...

Read More
Showing 4491–4500 of 8,392 articles
« Prev 1 448 449 450 451 452 840 Next »
Advertisements