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 408 of 840
When summing values from 2 arrays how can I cap the value in the new JavaScript array?
When working with arrays that represent RGB color values, you often need to add corresponding elements while ensuring the result doesn't exceed the maximum value of 255. This is a common requirement in graphics programming and color manipulation. Suppose we have two arrays, each containing three elements representing the red, green, and blue color values as integers. Our goal is to add the corresponding values to form a new RGB color array, capping any value that exceeds 255. Problem Statement We need to create a function that: Takes two arrays as input (representing RGB values) Adds ...
Read MoreSplit one-dimensional array into two-dimensional array JavaScript
We are required to write a function that takes in a one-dimensional array as the first argument and a number n as the second argument and we have to make n subarrays inside of the parent array (if possible) and divide elements into them accordingly. If the array contains 9 elements and we asked to make 4 subarrays, then dividing 2 elements in each subarray creates 5 subarrays and 3 in each creates 3, so in such cases we have to fallback to nearest lowest level (3 in ...
Read MoreSplit string into equal parts JavaScript
In JavaScript, splitting a string into equal parts can be accomplished in several ways. This article demonstrates how to split a string into n equal parts using an alternating pattern that takes characters from both ends of the string. Problem Statement We need to write a JavaScript function that takes a string and a number n as arguments, where n exactly divides the string length. The function should return an array of n strings of equal length, formed by alternating between the first and last characters of the remaining string. For example: If the string ...
Read MoreSum of distinct elements of an array - JavaScript
We are required to write a JavaScript function that takes in one such array and counts the sum of all distinct elements of the array. For example: Suppose, we have an array of numbers like this − const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1]; The distinct elements are: 1, 5, 2, 3, 4, 7, 8. Their sum is: 1 + 5 + 2 + 3 + 4 + 7 + 8 = 30. Using lastIndexOf() Method This approach checks if the current index matches the ...
Read MoreHow to create an Autocomplete with JavaScript?
Creating an autocomplete feature enhances user experience by providing real-time suggestions as users type. This implementation uses vanilla JavaScript to filter and display matching results from a predefined array. HTML Structure The autocomplete requires a wrapper div with the autocomplete class and an input field: Complete Example * { box-sizing: border-box; ...
Read MoreHow to disable default behavior of when you press enter with JavaScript?
To disable the default behavior when the Enter key is pressed, you need to use the keydown event listener along with preventDefault(). This prevents the browser from executing its default action for the Enter key. How It Works The preventDefault() method cancels the default action that belongs to the event. When combined with checking for the Enter key (event.key === "Enter"), it stops the browser's default behavior like form submission or input validation. Example Disable Enter Key Default ...
Read MoreJavaScript to parse and show current time stamp of an HTML audio player.
The HTML audio element provides a currentTime property that returns the current playback position in seconds. We can parse this value to display minutes and seconds separately. HTML Audio currentTime Property The currentTime property returns a floating-point number representing the current playback time in seconds. We can use Math.floor() to convert it into readable minutes and seconds format. Example Audio Timestamp Parser body { ...
Read MoreJavaScript array.includes inside nested array returning false where as searched name is in array
When working with nested arrays in JavaScript, the standard includes() method only checks the first level of the array. This article explores why this happens and provides a simple solution using JSON.stringify() to search through multidimensional arrays. The Problem The Array.prototype.includes() method only performs shallow comparison, meaning it cannot find elements nested within sub-arrays: const names = ['Ram', 'Shyam', ['Laxman', 'Jay']]; console.log(names.includes('Ram')); // true - found at first level console.log(names.includes('Laxman')); // false - nested inside sub-array true false Solution: Using JSON.stringify() A simple approach ...
Read MoreJavaScript R- eturn Array Item(s) With Largest Score
We have an array of arrays that contains the marks scored by some students in different subjects. We need to write a function that returns the top scorer(s) for each subject, handling cases where multiple students have the same highest score. Problem Setup Given this input data: const arr = [ ['Math', 'John', 100], ['Math', 'Jake', 89], ['Math', 'Amy', 93], ['Science', 'Jake', 89], ['Science', 'John', 89], ['Science', 'Amy', 83], ...
Read MoreReplace array value from a specific position in JavaScript
To replace a value at a specific position in a JavaScript array, you can use the splice() method or direct index assignment. Both approaches modify the original array. Using splice() Method The splice() method removes elements and optionally adds new ones at a specified position. Syntax array.splice(index, deleteCount, newElement) Example var changePosition = 2; var listOfNames = ['John', 'David', 'Mike', 'Sam', 'Carol']; console.log("Before replacing:"); console.log(listOfNames); var name = 'Adam'; var result = listOfNames.splice(changePosition, 1, name); console.log("After replacing:"); console.log(listOfNames); console.log("Removed element:", result); Before replacing: [ 'John', ...
Read More