Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 422 of 840
Iterating and printing a JSON with no initial key and multiple entries?
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 MoreChange every letter to next letter - JavaScript
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 MoreJavaScript code to print last element of an array
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 MoreReturning an array with the minimum and maximum elements JavaScript
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 MoreFinding content of arrays on the basis of specific property in JavaScript?
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 MoreRepeating letter string - JavaScript
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 MoreFill missing numeric values in a JavaScript array
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 MoreHow can I cut a string after X characters in JavaScript?
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 MoreHow to create an increment of 10 value once you click a button in JavaScript?
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 MoreFinding missing letter in a string - JavaScript
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