AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 422 of 840

Iterating and printing a JSON with no initial key and multiple entries?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 104 Views

When working with JSON arrays containing multiple objects, you can iterate through each entry using JavaScript's forEach() method. This is particularly useful when your JSON data doesn't have a single root key and contains multiple entries. Syntax array.forEach((element, index) => { // Process each element }); Example: Iterating Through Student Records const details = [ { "studentId": 101, "studentName": "John Doe" }, ...

Read More

Change every letter to next letter - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 799 Views

We are required to write a JavaScript function that takes in a string and changes every letter of the string from the English alphabets to its succeeding element. For example: If the string is − const str = 'how are you'; Then the output should be − const output = 'ipx bsf zpv'; How It Works The algorithm processes each character by checking if it's a letter using ASCII codes. For letters 'a-y' and 'A-Y', it moves to the next letter. For 'z' and 'Z', it wraps around to 'a' and 'A' respectively. Non-alphabetic ...

Read More

JavaScript code to print last element of an array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 278 Views

Getting the last element of an array is a common operation in JavaScript. There are several methods to accomplish this, with different approaches depending on whether you want to modify the original array or not. Using Array Length (Recommended) The most efficient method uses the array's length property to access the last index: Last Array Element Array: ["A", 1, 2, 3, "B", "D"] Get Last Element ...

Read More

Returning an array with the minimum and maximum elements JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

Finding the minimum and maximum elements in a JavaScript array is a common task. This tutorial shows how to return both values in a single array using different approaches. const arr = [12, 54, 6, 23, 87, 4, 545, 7, 65, 18, 87, 8, 76]; We need to write a function that finds the minimum and maximum elements and returns them as an array with minimum at index 0 and maximum at index 1. Using Array.reduce() Method The reduce() method provides an efficient way to traverse the array once and track both minimum and ...

Read More

Finding content of arrays on the basis of specific property in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 183 Views

When working with arrays of objects in JavaScript, you often need to find and merge content based on matching properties. The find() method combined with map() provides an effective solution for this common task. Understanding the Problem Consider two arrays of student records where you want to find matching students based on their roll numbers and merge or replace data from the second array when matches are found. Using map() with find() The map() method creates a new array by transforming each element, while find() searches for the first matching element in another array. ...

Read More

Repeating letter string - JavaScript

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

We are required to write a JavaScript function that takes in a string and a number, say n, and the function should return a new string in which all the letters of the original string are repeated n times. For example: If the string is − const str = 'how are you' And the number n is 2 Then the output should be − const output = 'hhooww aarree yyoouu' Example Following is the code − const str = 'how are you'; const repeatNTimes ...

Read More

Fill missing numeric values in a JavaScript array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 591 Views

We are given an array of n entries, of which only 2 are Numbers, all other entries are null. Something like this: const arr = [null, null, -1, null, null, null, -3, null, null, null]; We are supposed to write a function that takes in this array and complete the arithmetic series of which these two numbers are a part. To understand this problem more clearly, we can think of these null values as blank space where we need to fill numbers so that the whole array forms an arithmetic progression. About Arithmetic Progression ...

Read More

How can I cut a string after X characters in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 756 Views

To cut a string after X characters in JavaScript, you can use several methods. The most common approaches are substr(), substring(), and slice(). Using substr() Method The substr() method extracts a portion of a string, starting at a specified index and extending for a given number of characters. var myName = "JohnSmithMITUS"; console.log("The String = " + myName); var afterXCharacter = myName.substr(0, 9); console.log("After cutting the characters the string = " + afterXCharacter); The String = JohnSmithMITUS After cutting the characters the string = JohnSmith Using substring() Method The substring() ...

Read More

How to create an increment of 10 value once you click a button in JavaScript?

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

To create a button that increments a value by 10 each time it's clicked, you can use JavaScript's click() event listener along with parseInt() to handle numeric operations. Basic Approach The core concept involves storing a counter variable, adding 10 to it on each button click, and updating the display element with the new value. Example Increment by 10 10 0 ...

Read More

Finding missing letter in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 760 Views

We have a string of length m that contains first m letters of the English alphabets, but somehow, one element went missing from the string. So, now the string contains, m-1 letters We are required to write a function that takes in one such string and returns the missing element from the string. Example Following is the code − const str = "acdghfbekj"; const missingCharacter = str => { // to make the function more consistent const s = str.toLowerCase(); ...

Read More
Showing 4211–4220 of 8,392 articles
« Prev 1 420 421 422 423 424 840 Next »
Advertisements